Skip to content

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

@nkglasser

Description

@nkglasser

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions