Why is EF returning a proxy class instead of the actual entity?

后端 未结 6 1481
时光取名叫无心
时光取名叫无心 2020-12-02 20:24

I\'m having trouble with entity framework returning Proxies when I want the actual entity class. The first time I run my code everything runs properly (no proxies), but eve

6条回答
  •  情歌与酒
    2020-12-02 21:06

    You can set ObjectContext.ContextOptions.ProxyCreationEnabled to false. This will prevent you from using some of EFs fancy features like lazy loading and I believe change tracking.

    As far as your app cares, it should be able to treat the proxies just like the types they represent. Is there a specific issue you are having?

    Edit

    We have some code that requires the POCO type instead of the proxy type and we do the following to detect if the current type is a proxy.

    if (entityType.BaseType != null && entityType.Namespace == "System.Data.Entity.DynamicProxies")
    {
        entityType = entityType.BaseType;
    }
    

提交回复
热议问题