Search code examples
c#entity-frameworkef-database-first

"the entity type is not part of the model for the current context" error is thrown when project contains more than one EDMX file


I'm using database first and I have a switch statement that looks something like this:

switch (site)
{
    case Site.One:
        using (OneContext one = new OneContext())
            return one.OrganizationObjects.SingleOrDefault(x => x.u_Name == orgName)?.g_org_id;
    case Site.Two:
        using (TwoContext two = new TwoContext())
            return two.OrganizationObjects.SingleOrDefault(x => x.u_Name == orgName)?.g_org_id;
    default:
        throw new NotImplementedException();
}

Both databases are pretty similar and have almost the all of the same models.

If I delete the "Two" EDMX file and comment out the condition, then OneContext works perfectly.
If I add the TwoContext EDMX file to the project and run the code again, the "OneContext" code fails when it tries to query OrganizationObjects.

I made sure each context was using the correct connection string, but this error still occurs:

enter image description here


Solution

  • Workaround: Change a property on one of the two identical classes.

    EF matches on class name AND class properties. So I just changed a property name on one of the EF objects, and the error is gone.

    As @Entrodus commented on one of the other answers:

    EF collision happens only when two classes have the same name AND the same set of parameters.

    The mapping of CLR type to EDM type is ambiguous with EF 6 & 5?