Entity Framework 엔터티 연결을 사용하는 경우 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-TOM\ELS-RELEASE1\ELS.Service.Business\Base\ServiceBase.cs: line 98
ELS.Service.Business.IntegrationTest.Base.ServiceBaseIntegrationTest.StartLoggingTestMethod() in C:\C-TOM\ELS-RELEASE1\ELS.Service.Business.IntegrationTest\Base\ServiceBaseIntegrationTest.cs: line 65
그러나 동일한 연결 문자열을 사용하는 다음 코드 :
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="Data Source=localhost;Initial Catalog=els5_demo;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
나는 또한 리플렉터에서 dll을 열었고 메타 데이터는 괜찮아 보입니다.
문제를 찾았습니다.
표준 메타 데이터 문자열은 다음과 같습니다.
metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl
그리고 이것은 대부분의 경우 잘 작동합니다. 그러나 일부 (내 포함) Entity Framework에서는 혼란스럽고 어떤 dll을 찾을 지 모릅니다. 따라서 메타 데이터 문자열을 다음과 같이 변경하십시오.
metadata=res://nameOfDll/Model.csdl|res://nameOfDll/Model.ssdl|res://nameOfDll/Model.msl
그리고 그것은 작동 할 것입니다. 저를 올바른 길로 인도 한 것은이 링크였습니다.
http://itstu.blogspot.com/2008/07/to-load-specified-metadata-resource.html
나는 반대 문제가 있었지만 단위 테스트에서는 작동하지 않았지만 서비스에서 일했습니다.
동일한 오류 메시지가 있었고 문제는 연결 문자열의 메타 데이터 부분이기도했지만 문제를 해결하기 위해 조금 더 깊이 파고 들어이 작은 덩어리를 공유하고 싶었습니다.
메타 데이터 문자열은 각각 다음과 같은 세 섹션으로 구성됩니다.
res://
(assembly)/
(model name).(ext)
여기서 ext 는 "csdl", "ssdl"및 "msl"입니다.
대부분의 사람들에게 어셈블리 는 아마도 "*"일 수 있으며, 이는로드 된 모든 어셈블리가 검색 될 것임을 나타냅니다 (이에 대해 많은 테스트를 수행하지 않았습니다). 이 부분은 나에게 문제가되지 않았기 때문에 어셈블리 이름이 필요한지 파일 이름이 필요한지 (즉, ".dll"이 있는지 여부에 관계없이) 언급 할 수 없습니다. 둘 다 제안한 것을 보았습니다.
The model name part should be the name and namespace of your .edmx file, relative to your assembly. So if you have a My.DataAccess assembly and you create DataModels.edmx in a Models folder, its full name is My.DataAccess.Models.DataModels. In this case, you would have "Models.DataModels.(ext)" in your metadata.
If you ever move or rename your .edmx file, you will need to update your metadata string manually (in my experience), and remembering to change the relative namespace will save a few headaches.
There are several possible catches. I think that the most common error is in this part of the connection string:
res://xxx/yyy.csdl|res://xxx/yyy.ssdl|res://xxx/yyy.msl;
This is no magic. Once you understand what is stands for you'll get the connection string right.
First the xxx part. That's nothing else than an assembly name where you defined you EF context clas. Usually it would be something like MyProject.Data. Default value is * which stands for all loaded assemblies. It's always better to specify a particular assembly name.
Now the yyy part. That's a resource name in the xxx assembly. It will usually be something like a relative path to your .edmx file with dots instead of slashes. E.g. Models/Catalog - Models.Catalog The easiest way to get the correct string for your application is to build the xxx assembly. Then open the assembly dll file in a text editor (I prefer the Total Commander's default viewer) and search for ".csdl". Usually there won't be more than 1 occurence of that string.
Your final EF connection string may look like this:
res://MyProject.Data/Models.Catalog.DataContext.csdl|res://MyProject.Data/Models.Catalog.DataContext.ssdl|res://MyProject.Data/Models.Catalog.DataContext.msl;
As Shiraz Bhaiji answered, the metadata=res:///Model.csdl|res:///Model.ssdl|res://*/Model.msl was the case. However I still had problems with constructing the proper string based on my Model localization, namespaces and assemby name. The very simple solution was to rename the .edmx file in Visual Studio(after than rename and get back to the original name), which triggered the automatic refreshing of the string in my Web.config
I had the same problem with three projects in one solution and all of the suggestions didn't work until I made a reference in the reference file of the web site project to the project where the edmx file sits.
I moved my Database First DataModel to a different project midway through development. Poor planning (or lack there of) on my part.
Initially I had a solution with one project. Then I added another project to the solution and recreated my Database First DataModel from the Sql Server Dataase.
To fix the problem - MetadataException when using Entity Framework Entity Connection. I copied my the ConnectionString from the new Project Web.Config to the original project Web.Config. However, this occurred after I updated my all the references in the original project to new DataModel project.
It might just be a connection string error, which is solved by the above process, but if you are using the dll's in multiple projects then making sure the connection string is named properly will fix the error for sure.
I had this problem when moving my .edmx
database first model from one project to another.
I simply did the following:
- Deleted the connection strings in the
app.config
orweb.config
- Deleted the 'Model.edmx'
- Re-added the model to the project.
'program story' 카테고리의 다른 글
Windows 업데이트 후“ 'System.Web.Mvc'네임 스페이스에 'Html'유형 또는 네임 스페이스 이름이 없습니다.” (0) | 2020.11.13 |
---|---|
.NET에서 디렉토리 크기를 계산하는 가장 좋은 방법은 무엇입니까? (0) | 2020.11.13 |
java.security.Signature 대 MessageDigest 및 Cipher와 함께 SHA1 및 RSA 사용 (0) | 2020.11.12 |
PHP : __ ( 'Some text')는 무엇을합니까? (0) | 2020.11.12 |
큰 파이썬 프로젝트에서 죽은 코드 찾기 (0) | 2020.11.12 |