ObjectContext.GetObjectType(e.GetType()) not returning the entity type of the POCO entity

c# entity-framework entity-framework-6

Question

The ObjectContext.GetObjectType Method should return "the entity type of the POCO entity associated with a proxy object of a specified type"

So how come in my code it just returns the proxy?

I am using entity framework 6 release candidate

        //Soft delete
        var e = Context.Set<T>().Find(id);
        e.IsDeleted = true;
        InsertOrUpdate(e);

        Type t = System.Data.Objects.ObjectContext.GetObjectType(e.GetType());
        string name = t.Name;
        //Property_6C887DE7274181E6E99D6FCF2C21BDD59E226F99B0064F59954E70062C135331

        //Surely I shouldn't have to use Substring here?
        name = name.Substring(0, name.IndexOf("_")).ToSpacedTitleCase();
        string message = name + " deleted";

Accepted Answer

You are using the wrong ObjectContext. EF6 is not built on System.Data.Entity.dll. You need to use ObjectContext from EntityFramework.dll. It's actually not recommended to have a reference to System.Data.Entity.dll in your project at all if you are using EF6 to avoid situation like this.

To fix your issue:

  • remove the reference to System.Data.Entity.dll (just in case)
  • replace System.Data.Objects.ObjectContext.GetObjectType(e.GetType()); with System.Data.Entity.Core.Objects.ObjectContext.GetObjectType(e.GetType())



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why