Is an EDM type CLR mapping ambiguous with EF 6 and 5?

Please can someone help me fix this error?

The specified schema is invalid. Errors:

The mapping of the CLR type of the EDM type is ambiguous, since several CLR types correspond to the EDM type of 'City_DAL'. The previously found CLR type is "CeossDAL.City_DAL", the new CLR type is "CeossBLL.City_DAL".

The main problem is that I have a DAL and it contains EF and BLL, and it contains the same DAL classes, but differs in the namespace, and that is what causes the problem

I do not know how to get rid of this problem, can you help me?

I would also appreciate if someone would give me a sample for using the n-tier architecture with EF

thank

+61
c # entity-framework entity-framework-4
Feb 17 '13 at 23:30
source share
14 answers

Do not use classes with the same unqualified name. EF uses only class names to identify the type displayed in EDMX (namespaces are ignored) - this is an agreement that allows classes from different namespaces to be mapped to a single model. The solution to your problem is to name your classes differently in BLL.

+72
Feb 18 '13 at 19:37
source share

Workaround: Change the property to one of two identical classes.

EF matches class and class properties. So I just changed the name of the property on one of the EF objects and the error went away.

As @Entrodus commented on one of the other answers:

An EF collision occurs only when two classes have the same name. the same set of parameters.

+36
Oct. 16 '15 at 16:27
source share

This question may be helpful. This involves placing the BLL and DAL classes in separate assemblies.

+10
Mar 06 '13 at 8:47
source share

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 the line as follows:

<EntityType Name="CartItem"> 

to that:

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

or use this if you have an 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 the business object.

+5
Jul 05 '17 at 16:16
source share

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

eg. The error that brought me here was caused by the fact that 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 over each element and assign it a type.

+4
Feb 08 '17 at 9:29 on
source share

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

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

0
Jan 31 '17 at 23:04 on
source share

Another reason you might get this error: If you load custom assemblies using Assembly.LoadFile with edmx files that are already loaded into memory. This creates repeating classes that don't need frame structures.

0
Dec 21 '17 at 21:02
source share

I got the error above because for both connection strings I had the same value for the metadata specified in my main project configuration file, as shown 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" /> 

As a result, I copied the correct connection string from the EntitiesB project configuration file.

0
Feb 02 '18 at 4:46
source share

For me, this was because I was trying to access a type with the same name in the wrong context instance.

Let's say ContextA and ContextB have SomeType . I tried to access ContextA.SomeType on an instance of ContextB .

0
May 13 '18 at 17:19
source share

Just add EntityFramework as “Code from Database First” and not as “EF Designer from database”. This solved my problem, but it has a dark side: if you change the database, you need to delete all the classes and add it again, or just edit the classes, I use the latter when I change the properties of the columns, for example, "Allow". null "or row size. But if you add columns, I recommend deleting and adding classes again.

0
Aug 13 '18 at 16:53
source share

I managed to solve this problem without renaming classes, properties or metadata.

I had a project setup with a T4 transform that creates entity objects in a DAL project and a T4 transform that creates domain objects in a Domain project, and both referenced EDMX to create identical objects, and then I mapped DAL objects to Domain objects.,

The error occurred only when I referred to other classes (enumerations in my case) from the Domain assembly in my queries. When I removed them, the error was gone. It seems that EF was loading my domain assembly because of this, saw other classes with the same name, and exploded.

To solve this problem, I made a separate assembly that contained only my converted T4 domain classes. Since I never need to use them inside a query (only after a query for matching), I no longer have this problem. It seems cleaner and simpler than the answers below.

0
Oct 02 '18 at 17:29
source share

if you have 2 connection strings in the web configuration, but you want to use one connection string, you use the dynamic creation of the connection string for other objects. I have edmx (db first), and first encode the entities in my solution. I use this class in first-person code.

 using System; using System.Collections.Generic; using System.Configuration; using System.Data.Entity.Core.EntityClient; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Data { public class SingleConnection { private SingleConnection() { } private static SingleConnection _ConsString = null; private String _String = null; public static string ConString { get { if (_ConsString == null) { _ConsString = new SingleConnection { _String = SingleConnection.Connect() }; return _ConsString._String; } else return _ConsString._String; } } public static string Connect() { string conString = ConfigurationManager.ConnectionStrings["YourConnectionStringsName"].ConnectionString; if (conString.ToLower().StartsWith("metadata=")) { System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder efBuilder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(conString); conString = efBuilder.ProviderConnectionString; } SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder(conString); string dataSource = cns.DataSource; SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder() { DataSource = cns.DataSource, // Server name InitialCatalog = cns.InitialCatalog, //Database UserID = cns.UserID, //Username Password = cns.Password, //Password, MultipleActiveResultSets = true, ApplicationName = "EntityFramework", }; //Build an Entity Framework connection string EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder() { Provider = "System.Data.SqlClient", Metadata = "res://*", ProviderConnectionString = sqlString.ToString() }; return entityString.ConnectionString; } } } 

And when I call legal entities

 private static DBEntities context { get { if (_context == null) _context = new DBEntities(SingleConnection.ConString); return _context; } set { _context = value; } } 
0
Dec 13 '18 at 9:54
source share

I think that you have a class X named "MyClass" in object models, and another class called "MyClass" in the same WorkFolder or Extended first class. This is my problem and I am fixing it.

0
Feb 01 '19 at 12:53 on
source share

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

 Mapper.CreateMap<Model.FileHistoryEFModel, DataTypes.FileHistory>(); Mapper.CreateMap<DataTypes.FileHistory, Model.FileHistoryEFModel>(); 
-8
Feb 09 '15 at 12:07
source share



All Articles