You can get the original entity type of a proxy type by
ObjectContext.GetObjectType(entity.GetType())
This is a static method of ObjectContext
, so you can readily use in in a DbContext
environment.
If for some reason you need the actual entity as its original type you can use the pattern
var entity = entry.Entity as MyEntity;
if (entity != null)
{
...
}
This is slightly more efficient than
if (entry.Entity is MyEntity)
{
var entity = (MyEntity)entry.Entity;
...
}
because the latter snippet casts the object twice.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…