Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ModelMetadata for a class in .NET Core

In Entity Framework 6 I could get the ModelMetadata for a class (myModel) like this:

var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, myModel.GetType());

How can I do the same in .net core 1.1.1?

like image 441
simon of earth Avatar asked Jan 04 '23 06:01

simon of earth


1 Answers

ModelMetadataProvider is an ASP.NET feature and it has nothing to do with Entity Framework. In ASP.NET Core you may easily inject a default IModelMetadataProvider implementation using the built-in DI:

public FooController(IModelMetadataProvider provider) 
{
    var metadata = provider.GetMetadataForType(typeof(model));
}
like image 137
Ilya Chumakov Avatar answered Jan 13 '23 13:01

Ilya Chumakov