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

后端 未结 6 1494
时光取名叫无心
时光取名叫无心 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条回答
  •  猫巷女王i
    2020-12-02 21:19

    In EF 6.1.3 you can get the right type using

    using (var context = new BloggingContext()) { 
        var blog = context.Blogs.Find(1); 
        var entityType = ObjectContext.GetObjectType(blog.GetType()); 
    }
    

    Note that if the type passed to GetObjectType is an instance of an entity type that is not a proxy type then the type of entity is still returned. This means you can always use this method to get the actual entity type without any other checking to see if the type is a proxy type or not.

    From MSDN

提交回复
热议问题