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

2

7

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

Oodles answered 28/12, 2017 at 17:21 Comment(7)
Both edmx is in same folder ?Dibri
@Dibri Yeah. It's multiple EDMXs in the same folder. Should they be in different folders if the models are similar?Oodles
Could you try separating these multiple folders. I had a problem like if second edmx is added i got missing the first edmx tt classes. it worked when seperated.Dibri
@Dibri Thanks for the advice. Testing now.Oodles
@Dibri Moving the EDMX files to their own folders didn't fix the problem. But it gave me a new error that is a little less cryptic. It boils down to the issue of having the same entity name in multiple contexts: The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM typeOodles
@Dibri I was finally able to resolve the issue. This was the answer: https://mcmap.net/q/237893/-the-mapping-of-clr-type-to-edm-type-is-ambiguous-with-ef-6-amp-5. I was able to add a field to the tables to make the objects unique.Oodles
if you have resolved it post here tooDibri
O
4

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?

Oodles answered 5/1, 2018 at 19:23 Comment(0)
D
-1

Entity Framework matches on class name and class properties. Two classes with the same class name and same properties results in conflict.

Change a property on one of the two identical classes will solve the issue.

Diseur answered 5/1, 2018 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.