【发布时间】:2017-05-10 09:10:42
【问题描述】:
我在 SQL Server 上使用实体框架工作了几年,但这个错误对我来说是新的。
我使用 EDMX 模型创建了一个库项目 (DLL)。
然后,我通过一个小控制台应用程序对其进行了测试,它可以正常工作。
最后,我尝试在需要这个 DLL 的实际项目中使用它,结果……它显然不起作用!!
我得到的错误是:
System.Data.Entity.Core.MetadataException:指定的架构不是 有效的。错误:错误 0194:将所有工件加载到 ItemCollection 必须具有相同的版本。有多个版本 遇到过。
完整的stackTrace是:
System.Data.Entity.Core.MetadataException:指定的架构无效。错误: 错误 0194:加载到 ItemCollection 中的所有工件必须具有相同的版本。遇到了多个版本。 在 System.Data.Entity.Core.Metadata.Edm.EdmItemCollection.LoadItems(IEnumerable
1 xmlReaders, IEnumerable1 sourceFilePaths,SchemaDataModelOption dataModelOption,DbProviderManifest providerManifest,ItemCollection itemCollection,布尔 throwOnError) 在 System.Data.Entity.Core.Metadata.Edm.EdmItemCollection.Init(IEnumerable1 xmlReaders, IEnumerable1 filePaths,布尔 throwOnError) 在 System.Data.Entity.Core.Metadata.Edm.EdmItemCollection..ctor(IEnumerable1 xmlReaders, IEnumerable1 filePaths,布尔跳过初始化) 在 System.Data.Entity.Core.Metadata.Edm.MetadataCache.LoadEdmItemCollection(MetadataArtifactLoader 加载器) 在 System.Data.Entity.Core.Metadata.Edm.MetadataCache.c__DisplayClass5.b__0(String k) 在 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) 在 System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetMetadataWorkspace(DbConnectionOptions EffectiveConnectionOptions) 在 System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace() 在 System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection 连接,布尔 isConnectionConstructor,ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory,Translator 转换器,ColumnMapFactory columnMapFactory) 在 System.Data.Entity.Internal.InternalConnection.CreateObjectContextFromConnectionModel() 在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型 entityType) 在 System.Data.Entity.Internal.Linq.InternalSet1.Initialize() in System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() 在 System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() in System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 源中,Expression1 predicate) in MailGunSender.MailManager.SendMail(String from, List1 收件人,List1 ccs, List1 ccns,字符串主题,字符串文本,字符串路径附件)在 xxx:riga 261
当然,我已经在调用者项目中安装了实体框架(使用 Nuget),就像我为测试器控制台项目所做的那样。但在这里它给了我这个错误。你知道为什么吗?
我的连接字符串由以下代码管理:
public partial class Entities : DbContext
{
public Entities(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
public void Close()
{
this.Dispose();
}
public static string GetConnectionString(string server, string db, string user, string pwd)
{
string cn = "Server=" + server;
cn += ";Database=" + db;
cn += ";uid=" + user;
cn += ";Pwd=" + pwd;
var providerSB = new SqlConnectionStringBuilder(cn);
var efConnection = new EntityConnectionStringBuilder();
efConnection.Provider = "System.Data.SqlClient";
efConnection.ProviderConnectionString = providerSB.ConnectionString;
efConnection.Metadata = @"res://*";
return efConnection.ToString();
}
}
【问题讨论】:
-
你的调用者项目是什么?例如。 ASP.NET MVC,WinForms? - 打开 NuGet 管理器检查您在所有项目中安装的实体框架包版本是否相同
-
您有多个 .edmx 文件吗?您是否尝试过刷新或重新创建任何旧文件。
-
@Scrobi 我只有一个 edmx 文件,在 dll 中
-
@LukeTO'Brien 调用者项目是 Windows 服务,而测试调用者项目是控制台应用程序
-
所有项目和 dll 是否都针对同一版本的 .Net?
标签: c# .net entity-framework linq metadata