![](/img/trans.png)
[英]The mapping of CLR type to EDM type is ambiguous with EF 4 classes ARE in a separate assembly
[英]The mapping of CLR type to EDM type is ambiguous with EF 6 & 5?
請誰能幫助我解決此錯誤?
指定的架構無效。 錯誤:
CLR類型到EDM類型的映射不明確,因為多個CLR類型與EDM類型“ City_DAL”匹配。 先前找到的CLR類型為“ CeossDAL.City_DAL”,新近發現的CLR類型為“ CeossBLL.City_DAL”。
我有DAL的主要問題,其中包含EF和BLL,並且包含DAL的相同類,但名稱空間不同,這是導致問題的原因
我不知道該如何解決這些問題,請您能幫幫我嗎?
如果有人給我示例以在EF中使用n層架構,我也將不勝感激
謝謝
請勿使用具有相同非限定名的類-EF僅使用類名來標識EDMX中映射的類型(忽略名稱空間)-這是一種約定,允許將類從不同的名稱空間映射到單個模型。 解決您的問題的方法是在BLL中以不同的方式命名您的類。
解決方法:更改兩個相同類之一的屬性。
EF在類名和類屬性上匹配。 因此,我只是更改了EF對象之一的屬性名稱,該錯誤消失了。
當@Entrodus對其他答案之一發表評論時:
僅當兩個類具有相同的名稱和相同的參數集時,才會發生EF沖突。
這個 MSDN論壇問題可能會有所幫助。 建議將BLL和DAL類放在單獨的程序集中。
對於EF 6.x,我在https://github.com/aspnet/EntityFramework/issues/941上找到了一些注釋,並在解決方案中通過向EDM類型添加注釋來解決此問題。
我手動編輯了EDMX文件,並更改了如下代碼:
<EntityType Name="CartItem">
對此:
<EntityType Name="CartItem" customannotation:ClrType="EntityModel.CartItem">
或在其他地方已有類型的情況下使用此方法:
<EntityType Name="CartItem" customannotation:ClrType="MyApp.CartItem, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
其中EntityModel是用於我的EF模型的名稱空間,而MyApp是業務對象的名稱空間
在某些情況下,這比實際問題更具症狀。 對我來說,當我嘗試在Linq查詢中調用一個函數而不先調用.ToList()時,它通常會彈出。
例如,導致我出現此錯誤的原因是我這樣做:
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
});
我必須調用.ToList(),然后遍歷每個項目並為其分配類型。
提出問題時,這可能不可用,但是另一種解決方案是刪除EDMX,並將其重新創建為代碼優先實體數據模型。 在EF6中,使用代碼優先功能,您可以從不同的模型名稱空間映射兩個具有相同名稱的類,而不會產生沖突。
要在Visual Studio(2013)中創建實體數據模型,請轉到“添加”>“新建項...”>“ ADO.NET實體數據模型”。 確保選擇“來自數據庫的代碼優先”選項。
可能會出現此錯誤的另一個原因:如果要使用Assembly.LoadFile加載具有edmx文件的自定義程序集,這些自定義程序集已被加載到內存中。 這將創建實體框架不喜歡的重復類。
我收到上面的錯誤,因為對於兩個連接字符串,我在主項目的配置文件中指定的元數據具有相同的值,如下所示:
<add name="EntitiesA" connectionString="metadata=res://*/EntitiesA.csdl|res://*/EntitiesA.ssdl|res://*/EntitiesA.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=localhost;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
我最終從EntitiesB的項目的配置文件中復制了正確的連接字符串。
對我來說,這是因為我試圖在錯誤的上下文實例上訪問具有相同名稱的類型。
說ContextA
和ContextB
都具有SomeType
。 我試圖在ContextB
實例上訪問ContextA.SomeType
。
只需將EntityFramework添加為“來自數據庫的代碼優先”,而不添加為“來自數據庫的EF設計器”。 這解決了我的問題,但是有一個陰暗面,如果您更改數據庫,則必須刪除所有類並再次添加它,或者只是編輯類,當更改列的屬性時,我會使用最后一個,例如“允許null”或字符串的大小。 但是,如果您添加列,建議刪除並再次添加類。
我能夠解決此問題,而無需重命名類,屬性或元數據。
我通過T4轉換在DAL項目中創建實體對象並在T4轉換在Domain項目中創建域對象的項目設置中,都引用EDMX來生成相同的對象,然后將DAL對象映射到Domain對象。
僅當我在查詢中從Domain程序集引用其他類(以我的情況為枚舉)時,才發生該錯誤。 當我刪除它們時,錯誤消失了。 因此,EF似乎正在加載我的Domain程序集,看到其他同名的類,然后崩潰。
為了解決這個問題,我制作了一個單獨的程序集,其中僅包含T4轉換的Domain類。 由於我不再需要在查詢中使用這些內容(僅在要映射到的查詢之后),因此不再有此問題。 這似乎比下面的答案更干凈,更容易。
如果您在Web配置中有2個連接字符串,但是要使用一個連接字符串,則可以使用動態創建連接字符串的其他實體。 我在解決方案中有edmx(db首先)和代碼優先實體。 我在Code first實體中使用此類。
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;
}
}
}
當我稱實體
private static DBEntities context
{
get
{
if (_context == null)
_context = new DBEntities(SingleConnection.ConString);
return _context;
}
set { _context = value; }
}
我認為您在實體模型中有一個名為“ MyClass”的類X,並且在第一個類的同一WorkFolder或Extended中具有另一個稱為“ MyClass”的類。 那是我的問題,我解決了。
您可以下載一個名為AutoMapper的庫。 它可以幫助您定義從一種類型到另一種類型的類映射。
Mapper.CreateMap<Model.FileHistoryEFModel, DataTypes.FileHistory>();
Mapper.CreateMap<DataTypes.FileHistory, Model.FileHistoryEFModel>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.