I'm migrating a project from EF6 to EF-Core. The Metadata API has changed significantly and I am unable to find a solution to this:
Under EF6 I could find the POCO Type from the Proxy Type using:
ObjectContext.GetObjectType(theEntity.GetType)
This, however, does not work under EF-Core (no ObjectContext
class). I've searched and searched to no avail. Does anyone know how to get the POCO type from either the entity
or the entity proxy type
?
EF Core does not support the ObjectContext
API. Further more, EF Core doesn't have proxy types.
You can get metadata about entity types from IModel
.
using (var db = new MyDbContext())
{
// gets the metadata about all entity types
IEnumerable<IEntityType> entityTypes = db.Model.GetEntityTypes();
foreach (var entityType in entityTypes)
{
Type pocoType = entityType.ClrType;
}
}
There is no perfect way. You could for example check the namespace. If it's a proxy it'll
private Type Unproxy(Type type)
{
if(type.Namespace == "Castle.Proxies")
{
return type.BaseType;
}
return type;
}