C# CLR 类型到 EDM 类型的映射与 EF 6 & 5 不明确?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14927391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:27:56  来源:igfitidea点击:

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

c#entity-frameworkentity-framework-4

提问by Mahmoud Samir

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 multiple CLR types match the EDM type 'City_DAL'. Previously found CLR type 'CeossDAL.City_DAL', newly found CLR type 'CeossBLL.City_DAL'.

指定的架构无效。错误:

CLR 类型到 EDM 类型的映射是不明确的,因为多个 CLR 类型与 EDM 类型“City_DAL”相匹配。之前发现CLR类型'CeossDAL.City_DAL',新发现CLR类型'CeossBLL.City_DAL'。

The main problem that I have DAL and this contains the EF and BLL and this contains the same classes of the DAL but differ in the namespace and this is what cause the problem

我有 DAL 的主要问题,它包含 EF 和 BLL,它包含 DAL 的相同类,但命名空间不同,这就是导致问题的原因

I can't know how to get rid of these problem, can you please help me?

我不知道如何摆脱这些问题,你能帮我吗?

Also I will be appreciated if some one give me sample to use n-tier architecture with EF

此外,如果有人给我示例以将 n 层架构与 EF 一起使用,我将不胜感激

Thank you

谢谢

回答by Ladislav Mrnka

Don't use classes with the same unqualifiedname - 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.

不要使用具有相同非限定名称的类 - EF 仅使用类名来标识在 EDMX 中映射的类型(命名空间被忽略) - 这是一种约定,允许将类从不同的命名空间映射到单个模型。您的问题的解决方案是在 BLL 中以不同的方式命名您的类。

回答by Jarno

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

这个MSDN 论坛问题可能会有所帮助。它建议将 BLL 和 DAL 类放在单独的程序集中。

回答by pkamathk

There is a library called AutoMapper which you can download. It helps you to define class mappings from one type to another.

您可以下载一个名为 AutoMapper 的库。它可以帮助您定义从一种类型到另一种类型的类映射。

Mapper.CreateMap<Model.FileHistoryEFModel, DataTypes.FileHistory>();
Mapper.CreateMap<DataTypes.FileHistory, Model.FileHistoryEFModel>();

回答by Matt

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.

EF 匹配类名和类属性。所以我只是更改了一个 EF 对象的属性名称,错误就消失了。

As @Entrodus commented on one of the other answers:

正如@Entrodus 对其他答案之一的评论:

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

EF 冲突仅在两个类具有相同名称和相同参数集时发生。

回答by Tawab Wakil

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.

当提出问题时,这可能不可用,但另一种解决方案是删除 EDMX 并将其重新创建为代码优先实体数据模型。在 EF6 中,通过代码优先,您可以映射来自不同模型命名空间的两个具有相同名称的类,而不会产生冲突。

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.

要在 Visual Studio (2013) 中创建实体数据模型,请转到“添加”>“新建项目...”>“ADO.NET 实体数据模型”。确保选择“Code First from database”选项。

回答by Captain Kenpachi

In some cases this is more of a symptom than the actual problem. For me, it usually pops up when I try to call a function inside a Linq query without calling .ToList() first.

在某些情况下,这更像是一种症状,而不是实际问题。对我来说,当我尝试在 Linq 查询中调用函数而不先调用 .ToList() 时,它通常会弹出。

E.g. the error that brought me here was caused because I did this:

例如,导致我来到这里的错误是因为我这样做了:

var vehicles = DB.Vehicles.Select(x => new QuickSearchResult()
{
    BodyText = x.Make + " " + x.Model + "<br/>"
    + "VIN: " + x.VIN + "<br/>"
    + "Reg: " + x.RegistrationNumber +"<br/>"
    + x.AdditionalInfo
    type = QuickSearchResultType.Vehicle,//HERE. Can't use an enum in an IQueryable.
    UniqueId = x.VehicleID
});

I had to call .ToList(), then iterate through each item and assign the type to it.

我必须调用 .ToList(),然后遍历每个项目并为其分配类型。

回答by Ekus

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

对于 EF 6.x,我在https://github.com/aspnet/EntityFramework/issues/941 上找到了一些注释,并通过向 EDM 类型添加注释在我的解决方案中解决了这个问题。

I edited the EDMX file manuallyand changed a line like this:

我手动编辑了 EDMX 文件并更改了如下一行:

<EntityType Name="CartItem">

to this:

对此:

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

oruse 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 EntityModelis the namespace used for my EF model, and MyAppis the namespace of a business object

其中EntityModel是用于我的 EF 模型的命名空间,而MyApp是业务对象的命名空间

回答by Thomas

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.

您可能会收到此错误的另一个原因:如果您正在加载带有已加载到内存中的 edmx 文件的 Assembly.LoadFile 的自定义程序集。这会创建实体框架不喜欢的重复类。

回答by Kristianne Nerona

I got the error above because for both connection strings, I had the same value for metadata specified in my main project's config file, like below:

我得到了上面的错误,因为对于两个连接字符串,我的主项目的配置文件中指定的元数据的值相同,如下所示:

<add name="EntitiesA" connectionString="metadata=res://*/EntitiesA.csdl|res://*/EntitiesA.ssdl|res://*/EntitiesA.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

<add name="EntitiesB" connectionString="metadata=res://*/EntitiesA.csdl|res://*/EntitiesA.ssdl|res://*/EntitiesA.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I ended up copying the correct connection string from the EntitiesB's project's config file.

我最终从 EntitiesB 的项目配置文件中复制了正确的连接字符串。

回答by Dave Cousineau

For me this was because I was attempting to access a type with the same name on the wrong context instance.

对我来说,这是因为我试图在错误的上下文实例上访问具有相同名称的类型。

Say both ContextAand ContextBhave SomeType. I was trying to access ContextA.SomeTypeon an instance of ContextB.

说都ContextAContextB拥有SomeType。我试图访问ContextA.SomeType上的一个实例ContextB