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

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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'.

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

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

Thank you

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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:

to this:

or use this if you have existing type elsewhere:

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



回答5:

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.



回答6:

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.

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 + "
" + "VIN: " + x.VIN + "
" + "Reg: " + x.RegistrationNumber +"
" + 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.



回答7:

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.



回答8:

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:

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



回答9:

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

Mapper.CreateMap(); Mapper.CreateMap(); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!