使用实体框架实体连接时的 MetadataException [英] MetadataException when using Entity Framework Entity Connection

查看:27
本文介绍了使用实体框架实体连接时的 MetadataException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码:

using (EntityConnection conn = new EntityConnection("name=ELSCommonEntities"))
{
  conn.Open();
}

给我以下错误:

Test method ELS.Service.Business.IntegrationTest.Base.ServiceBaseIntegrationTest.StartLoggingTestMethod threw exception:  System.Data.MetadataException: Unable to load the specified metadata resource..

使用以下堆栈跟踪:

System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
System.Data.EntityClient.EntityConnection.SplitPaths(String paths)
System.Data.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean initializeAllCollections)
System.Data.EntityClient.EntityConnection.InitializeMetadata(DbConnection newConnection, DbConnection originalConnection, Boolean closeOriginalConnectionOnFailure)
System.Data.EntityClient.EntityConnection.Open()
ELS.Service.Business.Base.ServiceBase.StartLogging(String userWindowsLogon) in C:C-TOMELS-RELEASE1ELS.Service.BusinessBaseServiceBase.cs: line 98
ELS.Service.Business.IntegrationTest.Base.ServiceBaseIntegrationTest.StartLoggingTestMethod() in C:C-TOMELS-RELEASE1ELS.Service.Business.IntegrationTestBaseServiceBaseIntegrationTest.cs: line 65

但是,这段代码使用了相同的连接字符串:

However, this code which uses the same connection string:

using (ELSCommonEntities db = new ELSCommonEntities())
{
    var res = from c in db.Logging
              select c;

    int i = res.Count();
}

不会报错.

连接字符串为:

<add name="ELSCommonEntities" connectionString="metadata=res://*/Common.CommonModel.csdl|res://*/Common.CommonModel.ssdl|res://*/Common.CommonModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=els5_demo;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

我还在反射器中打开了 dll,元数据看起来没问题.

I have also opened up the dll in reflector and the metadata looks ok.

推荐答案

发现问题.

标准元数据字符串如下所示:

The standard metadata string looks like this:

metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl

这在大多数情况下都可以正常工作.但是,在某些(包括我的)实体框架中会感到困惑,不知道要查看哪个 dll.因此,将元数据字符串更改为:

And this works fine in most cases. However, in some (including mine) Entity Framework get confused and does not know which dll to look in. Therefore, change the metadata string to:

metadata=res://nameOfDll/Model.csdl|res://nameOfDll/Model.ssdl|res://nameOfDll/Model.msl

它会起作用.正是这个链​​接让我走上了正轨:

And it will work. It was this link that got me on the right track:

http://itstu.blogspot.com/2008/07/to-load-specified-metadata-resource.html

虽然我有相反的问题,但没有在单元测试中工作,但在服务中工作.

Although I had the oposite problem, did not work in unit test, but worked in service.

这篇关于使用实体框架实体连接时的 MetadataException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆