How to get the Entity POCO type from the Entity Proxy type?

c# entity-framework-core

Question

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?

1
3
10/4/2019 2:09:30 PM

Accepted Answer

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;
    }
}
4
7/14/2016 4:02:59 PM

Popular Answer

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;
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow