Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 826
Sam Axe Avatar asked Dec 11 '22 16:12

Sam Axe


1 Answers

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;
}
like image 150
Mariusz Jamro Avatar answered Jan 02 '23 17:01

Mariusz Jamro