【发布时间】:2017-08-14 12:36:55
【问题描述】:
我已经为 Entity Framework 中的一个特殊问题摸索了好几个小时。
以下问题不重复,因为他们的回答对我没有帮助:
- MetadataException: Unable to load the specified metadata resource
- Entity Framework - Unable to load the specified metadata resource
- Unable to load the specified metadata resource
我有一个 aspnet core mvc 应用程序 (v1.1),它引用了一个 .NET Framework 4.7 项目 (Data Access Layer),并将 Entity Framework 6.1.3 作为 NuGet 包安装。
我正在使用设计器,所以我有一个 .edmx 文件。
我在运行时遇到以下异常:
System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource.
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.SplitPaths(String paths)
at System.Data.Entity.Core.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0()
at System.Data.Entity.Core.Common.Utils.Memoizer`2.Result.GetValue()
at System.Data.Entity.Core.Common.Utils.Memoizer`2.Evaluate(TArg arg)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetArtifactLoader(DbConnectionOptions effectiveConnectionOptions)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetMetadataWorkspace(DbConnectionOptions effectiveConnectionOptions)
at System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace()
at System.Data.Entity.Core.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection()
at System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator translator, ColumnMapFactory columnMapFactory)
at System.Data.Entity.Internal.InternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
我有以下连接字符串:
metadata=res://*/MyContext.csdl|res://*/MyContext.ssdl|res://*/MyContext.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=MyDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"
问题是,在Debug 构建中运行时,应用程序运行时没有任何问题。但是,在 Release 构建中运行时,会引发异常。但是,如果我在带有 .edmx 文件的项目上禁用 optimize code,则不再抛出异常。
我什至查看了Entity Framework source code. 你可以从 Stacktrace 中看到,这个异常在170 行被抛出,因为loaders.Count == 0。我不明白为什么不能在 Release 构建中从程序集中加载资源,而它在 Debug 构建中工作。
编辑 我刚刚安装了 Reflector 的试用版来检查组件。因此,当查看使用 Debug 配置构建的 .dll 文件时,我可以清楚地看到嵌入的 3 个资源文件。但是,在使用 Release 配置构建的程序集中,奇怪的是缺少资源文件!
【问题讨论】:
-
我遇到了一个非常相似的问题,我的设置和你的很相似——完整的框架实体框架项目,由.net core mvc app引用。如果我在 VS 中运行我的项目,或者将其部署到 Azure,一切都很好。如果我从命令行使用“dotnet run”,我偶尔会收到此错误。到目前为止,在 VS 中进行清理和重建已经摆脱了它。但是现在在我的两台开发机器中的一台上,每次使用“dotnet run”都会发生这种情况,但在 VS 中根本没有!将继续挖掘,但请更新这是你发现任何进一步的东西。
-
作为后续。我已经确认您所观察到的 - 当我使用 dotnet run 构建项目时,资源没有包含在实体程序集中。也许这是相同的根本问题。无论如何感谢您的帖子 - 我通过运行 VS 生成的 .exe 而不是使用 dotnet run (坚持重建实体程序集)来解决它。
-
这似乎是 dotnet CLI 中的一个错误:请参阅 this issue。很遗憾微软没有对此做出回应。
-
我也有这个问题。当我解决它时,我会尽量记住在这里回答。
标签: c# .net entity-framework