zzzprojects / GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
https://entityframework-graphdiff.net/overview
MIT License
333 stars 101 forks source link

InvalidOperationException: "The type System.Data.Entity.DynamicProxies.x is not known to the DbContext." #122

Closed nkglasser closed 4 years ago

nkglasser commented 9 years ago

While trying to rebuild a graph, my entity provided a relation to a lazy-loaded proxy.

In the EntityManager.cs file, there are multiple references of getting the type from an object. My solution was adding the ObjectContext.GetObjectType() function on the type provided (or the type of the object provided).

For example on line 75:

public IEnumerable<PropertyInfo> GetPrimaryKeyFieldsFor(Type entityType)
{ 
    var metadata = ObjectContext.MetadataWorkspace
            .GetItems<EntityType>(DataSpace.OSpace)
            .SingleOrDefault(p => p.FullName == ObjectContext.GetObjectType(entityType).FullName);
    if (metadata == null)
    {
        throw new InvalidOperationException(String.Format("The type {0} is not known to the DbContext.", entityType.FullName));
    }

    return metadata.KeyMembers
        .Select(k => entityType.GetProperty(k.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
        .ToList();
}
DudePascalou commented 9 years ago

I had the same error. In order to make it working, I made the choice to completely disable the use of proxies with EF:

public MyDbContext()
   : base("name=MyDbContext")
{
   this.Configuration.ProxyCreationEnabled = false;
}

I work with disconnected entities (ASP.Net MVC) so proxies are not useful for me.

JonathanMagnan commented 4 years ago

Hello all,

The fix for this issue has been released in v3.1.1,

If you believe we still have the issue, just let us known.

Best Regards,

Jon