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 as its original type instead of the proxy?
Alternatively I need to dynamically try to cast the proxy to the entity type. Here's a start.
var theEntityType = entityEntry.Entity;
if (
theEntityType.BaseType != null
&& entityType.Namespace == "System.Data.Entity.DynamicProxies"
)
theEntityType = entityType.BaseType;
// Now I need to cast to the correct type
// THIS WON'T WORK BECAUSE `theEntityType` is dynamic.
var entityObject = (theEntityType)entityEntry.Entity;
// My entites also don't implement IConvertible