dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.72k stars 3.17k forks source link

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type #941

Closed ches151 closed 2 years ago

ches151 commented 9 years ago

Schema specified is not valid. Errors: The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Crop'. Previously found CLR type 'AFI.Core.DataModels.gen2.Crop', newly found CLR type 'AFI.Core.DataModels.gen3.Crop'.

I get this exception at runtime when querying entities from a database via LINQ. In my project I use Database First and I have two classes with the same name but different namespaces. Each class belongs to a separate model and each model has its own connection string and thus own database.

This bug is at least two years old. The orginal bug has been posted by mousedoc here: http://entityframework.codeplex.com/workitem/483

Are there any plans on fixing this bug in the nearest future?

rowanmiller commented 9 years ago

Hey,

This is already supported in the EF7 code base. We didn't really do anything to explicitly support it, it was just a side effect of the lighterweight purpose built metadata system in EF7 :smile:.

~Rowan

davimack commented 8 years ago

Yes, but what about the poor slobs who have just gone through recreating every freaking entity model to get from EF4 to EF6 and now can't do anything but revert the changes? If it's not fixed in EF6, that's the only real option to get around this silly issue.

Closing this off doesn't seem right, since the issue is still in the codebase for EF6!

divega commented 8 years ago

@davimack This repo is for EF Core, so the issue was closed because there is nothing actionable that applies to EF Core. The bug remains open in the EF6 repo at http://entityframework.codeplex.com/workitem/483.

BTW, in one of the minor releases of EF6.x we added annotations that can be put in entity types in the EDMX to identify the CLR type they will be mapped to at runtime deterministically, which avoids scanning assemblies for all possible candidate CLR types and avoids the exception.

The EDMX needs to be edited manually to add the annotations since the tools won't add them. For a simple model with a Person entity, the ConceptualModels section of the EDMX would look like this:

<ConceptualModels>
    <Schema Namespace="ConsoleApplication33" 
        Alias="Self" 
        annotation:UseStrongSpatialTypes="false"
        xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" 
        xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" 
        xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
    <EntityType Name="Person" 
        customannotation:ClrType="MyApp.Person, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <Key>
        <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
        <Property Name="Name" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" />
    </EntityType>
    <EntityContainer Name="Town" customannotation:UseClrTypes="true">
        <EntitySet Name="People" EntityType="Self.Person" />
    </EntityContainer>
    </Schema>
</ConceptualModels>

Notice in particular the customannotation:ClrType on the Person entity type and the customannotation:UseClrTypes on the entity container.

davimack commented 8 years ago

Thank you, Diego. That should hopefully address my issue. Apologies for commenting in the wrong repo.

krptodr commented 6 years ago

How would you go about doing this with ComplexTypes?