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

后端 未结 14 1564
长发绾君心
长发绾君心 2020-11-27 05:38

Please any one can help me to fix this error?

Schema specified is not valid. Errors:

The mapping of CLR type to EDM type is ambiguous because mult

相关标签:
14条回答
  • 2020-11-27 06:25

    This MSDN forum question might be helpful. It suggest placing the BLL and DAL classes in separate assemblies.

    0 讨论(0)
  • 2020-11-27 06:25

    For EF 6.x, I found some notes at https://github.com/aspnet/EntityFramework/issues/941 and fixed this in my solution by adding annotation to the EDM type.

    I edited the EDMX file manually and changed a line like this:

    <EntityType Name="CartItem">
    

    to this:

    <EntityType Name="CartItem" customannotation:ClrType="EntityModel.CartItem">
    

    or use this if you have existing type elsewhere:

    <EntityType Name="CartItem" customannotation:ClrType="MyApp.CartItem, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
    

    where EntityModel is the namespace used for my EF model, and MyApp is the namespace of a business object

    0 讨论(0)
  • 2020-11-27 06:27

    This may not have been available when the question was asked, but another solution is to delete the EDMX and recreate it as a code-first entity data model. In EF6, with code-first, you can map two classes with the same name from different model namespaces without creating a conflict.

    To create the entity data model in Visual Studio (2013), go to "Add" > "New Item..." > "ADO.NET Entity Data Model". Be sure to choose the "Code First from database" option.

    0 讨论(0)
  • 2020-11-27 06:29

    Don't use classes with the same unqualified name - EF uses only class names to identify the type mapped in EDMX (namespaces are ignored) - it is a convention to allow mapping classes from different namespaces to single model. The solution for your problem is to name your classes in BLL differently.

    0 讨论(0)
  • 2020-11-27 06:29

    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.

    0 讨论(0)
  • 2020-11-27 06:31

    Another reason you might get this error: If you're loading custom assemblies with Assembly.LoadFile that have edmx files, that have already been loaded into memory. This creates duplicate classes that entity framework doesn't like.

    0 讨论(0)
提交回复
热议问题