unexpected GetType() result for entity entry

13,227

Solution 1

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.

Solution 2

You can use

Type t = entry.Entity.GetType().BaseType;

or

ObjectContext.GetObjectType(entity.GetType())

But the second way is a better way from my point of view. If you call Type() request inside a Mapper method, for example DTO mapper (from entity object to DTO class or from in-memory objects to DTO class), ObjectContext.GetObjectType(..) will grant you always the expected result contrary to what will .GetType().BaseType

For example, if you use a TPT (Table per Type) strategy for EF Entity Model, call BaseType() on in-memory object will return the base class in hierarchy contrary to what will ObjectContext.GetObjectType(..)

enter image description here

Solution 3

Another way is to access the BaseType property of the returned proxy type:

Type t = entry.Entity.GetType().BaseType;
Share:
13,227
Anton Putov
Author by

Anton Putov

Young .NET programmer . Interests - running (800 - 42000 meters) Univercity - Belorussian State Univercity (Faculty of radiophysics an computer technologies)

Updated on June 06, 2022

Comments

  • Anton Putov
    Anton Putov almost 2 years

    While I iterating through ObjectStateEntries I expected [t] variable name will be MY_ENTITY

    foreach (ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
    {
        Type t = entry.Entity.GetType();
        ...
    }
    

    but real I have

    System.Data.Entity.DynamicProxies.MY_ENTITY_vgfg7s7wyd7c7vgvgv.....
    

    How can I determine can I cast current entry to MY_ENTITY type?

  • nicodemus13
    nicodemus13 almost 10 years
    A very good reason for needing to know the original type is for overriding Equals(), as the standard GetType() returns the proxy, which is not the same as the underlying type.
  • Kate
    Kate over 8 years
    I initially went with this answer as it looks neater (more self contained than needing ObjectContext around) than the accepted answer. However, there is a potential problem depending on the scenario. If you are adding an entity, it's not a proxy type yet, but just a POCO type. Therefore, calling BaseType on it returns 'object'. If you are only working with proxy types, such as if it's just dealing with entities retrieved from DB then no problem, but if it could be POCO or proxy, such as if collection has unsaved entities, using ObjectConext is the only safe option as Enrico explains below.
  • madannes
    madannes over 7 years
    The (should have been) obvious fact that the method is static was completely lost on me until you pointed it out!
  • Vincent McNabb
    Vincent McNabb about 6 years
    Of course, now you can do if (entry.Entity is myEntity entity) { ... } which will declare and assign entity