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 it's 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
var entityObject = (theEntityType)entityEntry.Entity; // THIS WON'T WORK BECAUSE `theEntityType` is dynamic.
// My entites also don't implement IConvertible
See Question&Answers more detail:os