Get underlying entity object from entity framework proxy

前端 未结 6 1770
逝去的感伤
逝去的感伤 2020-12-04 16:41

I have an entity by getting it from DbEntityEntry.Entity. This returns the Entity Framework proxy for the entity.

How do I access the underlying object

6条回答
  •  天涯浪人
    2020-12-04 17:27

    If you end up needing to do this from a project that does not have access to EF or the DBContext, and you don't know if the type you are referencing is a proxy you can do something like this:

        public Type GetType
        {
            get
            {
                var thisType = _baseObject.GetType();
    
                if (thisType.Namespace == "System.Data.Entity.DynamicProxies")
                    return thisType.BaseType;
    
                return thisType;
            }
        }
    

提交回复
热议问题