简体   繁体   English

实体框架 - 无法加载指定的元数据资源

[英]Entity Framework - Unable to load the specified metadata resource

I realise that this has been asked a number of times but I just can't seem to get the bottom of my issue.我意识到这已经被问过很多次了,但我似乎无法弄清楚我的问题的根源。 I'm getting the following error stack:我收到以下错误堆栈:

在此处输入图片说明

When I reflect out my dll I can see the following当我反映我的 dll 时,我可以看到以下内容

在此处输入图片说明

Reading http://blogs.teamb.com/craigstuntz/2010/08/13/38628/ it suggests I'd expect to see the csdl, msl and ssdl files here, but they're not.阅读http://blogs.teamb.com/craigstuntz/2010/08/13/38628/它表明我希望在这里看到 csdl、msl 和 ssdl 文件,但它们不是。 They do exist here obj\\Debug\\edmxResourcesToEmbed though.不过,它们确实存在于 obj\\Debug\\edmxResourcesToEmbed 中。

Never-the-less I tried to explicitly tell the web.config where to look by doing this:无论如何,我尝试通过执行以下操作明确告诉 web.config 在哪里查看:

...connectionString="metadata=res://DllName.dll/PaymentModel.csdl|res://DllName.dll/PaymentModel.ssdl|res://DllName.dll/PaymentModel.msl;provider=System.Data.SqlClient;provider ... />

Which just throws an error saying it cannot find the dll:这只是抛出一个错误,说它找不到 dll:

Unable to resolve assembly 'DllName.dll'.无法解析程序集“DllName.dll”。

Very similar to this unresolved SO question Unable to resolve assembly Model.dll非常类似于这个未解决的 SO 问题Unable to resolve assembly Model.dll

The final thing I tried was to change the metadata line to:我尝试的最后一件事是将元数据行更改为:

...connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider ... />

This threw a message about sql ce which I'm not using - is there a way to get round this??这抛出了一条关于我没有使用的 sql ce 的消息 - 有没有办法绕过这个??

在此处输入图片说明

Is there anything else I can try?还有什么我可以尝试的吗? Or can anyone see where I'm going wrong?或者谁能​​看到我哪里出错了? Some extra detais:一些额外的细节:

  • Using EF 6 EDMX set to "Embedded Resource"使用 EF 6 EDMX 设置为“嵌入式资源”

  • Copy to output directory:"Do not copy"复制到输出目录:“请勿复制”

  • Metadata artifact processing: "Embed in output assembly"元数据工件处理:“嵌入输出程序集”

Finally on this - if I set the EDMX from Embedded Resource to EntityDeploy then this will work locally but not build on the build server as it throws the exact same error as this SO question:最后在这一点上 - 如果我将 EDMX 从 Embedded Resource 设置为 EntityDeploy 那么这将在本地工作但不会在构建服务器上构建,因为它抛出与这个 SO 问题完全相同的错误:

Could not find the Conceptual Schema node to embed as a resource for input file 找不到作为输入文件资源嵌入的概念架构节点

But the fix doesn't seem to help and I can't install .NET 4.5 on the server unfortunately.但该修复程序似乎没有帮助,而且很遗憾我无法在服务器上安装 .NET 4.5。

I have had the same problem.我曾经也有过一样的问题。 After moving .edmx file with EF model to separate assembly mentioned error caused my headache: "Unable to load the specified metadata resource".在将带有 EF 模型的 .edmx 文件移动到单独的程序集后,提到的错误引起了我的头痛:“无法加载指定的元数据资源”。

EF version 6.1英孚 6.1 版

Solution:解决方案:

Old value:旧值:

metadata=res://*/Data.DataModel.csdl

New value:新值:

metadata=res://*/DataModel.csdl

Initially .edmx was under project's folder and I have moved it into project's root.最初 .edmx 位于项目的文件夹下,我已将其移动到项目的根目录中。

So I've got to the bottom of this, partly I think this was my fault.所以我已经追根究底了,部分原因是我认为这是我的错。 I'll put solutions to each issue I encountered below in case it helps anyone else.我会为我在下面遇到的每个问题提供解决方案,以防它对其他人有所帮助。

Unable to load the specified metadata resource issue无法加载指定的元数据资源问题

This was caused by me setting the 'Metadata Artifact Processing' setting on the edmx model from "EntityDeploy" to "Embedded Resouce".这是因为我将 edmx 模型上的“元数据工件处理”设置从“EntityDeploy”设置为“Embedded Resouce”。

So this meant it just embedded the whole edmx file file into the dll and didn't generate the ssdl, msl and csdl files instead.所以这意味着它只是将整个 edmx 文件嵌入到 dll 中,而不是生成 ssdl、msl 和 csdl 文件。

I guess you have to set this to EntityDeploy for this to work and generate these files correctly.我想您必须将其设置为 EntityDeploy 才能正常工作并正确生成这些文件。 Makes perfect sense and our bad over here.完全有道理,我们在这里不好。

Unable to resolve assembly 'DllName.dll'无法解析程序集“DllName.dll”

This was resolved by Andrew in the comments above, thanks for the pointer on that.安德鲁在上面的评论中解决了这个问题,感谢您的指点。

Could not find the Conceptual Schema node to embed as a resource for input file找不到作为输入文件资源嵌入的概念架构节点

The key to this whole issue really is this, our build server is currently on Windows 2003, so can't have .NET 4.5 installed, locally we were using EF 6.1 running under .NET 4.0 on VS2013.整个问题的关键确实是这样,我们的构建服务器目前在 Windows 2003 上,所以不能安装 .NET 4.5,我们在本地使用 EF 6.1 在 VS2013 上的 .NET 4.0 下运行。

However for some reason it looks like we need to have .NET 4.5 installed on our build server to get this to build even though we're not using any of the 4.5 features and targetting the .NET 4 framework.然而,出于某种原因,即使我们没有使用任何 4.5 功能并针对 .NET 4 框架,我们似乎需要在我们的构建服务器上安装 .NET 4.5 才能构建它。

Downgrading to EF to 4.3 solved this issue for us in the short term.降级到 EF 到 4.3 在短期内为我们解决了这个问题。 Not ideal but it worked.不理想,但它奏效了。

you should specify the assembly fully qualified name and a path to your model file within it(separated by / ), not the dll name您应该指定程序集的完全限定名称和其中的模型文件的路径(以/分隔),而不是 dll 名称

connectionString="metadata=res://Name.Of.The.Assembly, Culture=neutral, PublicKeyToken=8e2e51fcf4cf363e/Payment.PaymentModel.csdl|.........."

the same for ssdl and msl ssdlmsl相同

Change the metaData更改元数据

connectionString="metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;

To

connectionString="metadata=res://*/;

The above change will fix the issue上述更改将解决问题

I was having the same issue我遇到了同样的问题

System.Data.MetadataException: Unable to load the specified metadata resource System.Data.MetadataException:无法加载指定的元数据资源

It turned out the Connection string which was generated earlier had all the edmx's name on it.原来之前生成的连接字符串上有所有 edmx 的名称。 I took out all the model names from the connection string leaving only the below string in my metadata我从连接字符串中取出所有模型名称,只在元数据中留下以下字符串

"metadata=res://*/;provider=System.Data.SqlClient;provider

This worked for me.这对我有用。

Solution :解决方案 :
1) Open .edmx file from visual studio. 1) 从 Visual Studio 打开 .edmx 文件。
2) Click anywhere. 2) 单击任意位置。
3) Should see edmx property window. 3) 应该看到 edmx 属性窗口。
4) Change Namespace to the correct folder name. 4) 将 Namespace 更改为正确的文件夹名称。
5) 1 : https://i.stack.imgur.com/6sZvo.png (select image for more details ). 5) 1 : https://i.stack.imgur.com/6sZvo.png (选择图片了解更多详情)。

If you're using the dotnet CLI如果您使用的是dotnet CLI

For newer visitors using the new dotnet build tooling to build Entity Framework 6.x based projects, know that it currently does NOT embed any metadata into the final build.对于使用新的dotnet build工具构建基于 Entity Framework 6.x 的项目的新访问者,请了解它目前不会将任何元数据嵌入到最终构建中。 So if you run from within VS it'll run, but if your CI scripts use dotnet then they will fail on the server until you switch back to msbuild .因此,如果您从 VS 内运行它会运行,但如果您的 CI 脚本使用dotnet那么它们将在服务器上失败,直到您切换回msbuild IMHO, this is a bug and the tools should handle this.恕我直言,这是一个错误,工具应该处理这个问题。 You can chime on the GitHub thread if needed.如果需要,您可以在 GitHub 线程中加入。

I actually received this identical error because my Entity Framework connection string was pointing to a valid database however it was the incorrrect database.我实际上收到了这个相同的错误,因为我的Entity Framework连接字符串指向一个有效的数据库,但它是不正确的数据库。

If you have the following instances in SQL Server:如果您在 SQL Server 中有以下实例:

  • MyDatabase1我的数据库1
  • MyDatabase2我的数据库2

Make sure that the connection string points to the same database that the .EDMX is wired up to and was generated from.确保连接字符串指向.EDMX连接到并从中生成的同一个数据库。 Seems obvious enough, but when copying EF connection settings from 1 project to another this error was made and the net result was the Unable to load the specified metadata resource message.似乎很明显,但是当将EF连接设置从 1 个项目复制到另一个项目时,发生了此错误,最终结果是Unable to load the specified metadata resource消息。 Once I used the correct connection string pointing to the correct database and assembly - the error was resolved.一旦我使用了指向正确数据库和程序集的正确连接字符串 - 错误就解决了。

The easiest thing to do is take the original connection string created in the .config file residing next to the .EDMX and copy where needed to make sure subtle or minor omissions/mistakes are not made causing EF to not load/connect correctly.最简单的方法是获取在.EDMX旁边的.config文件中创建的原始连接字符串,并在需要的.EDMX复制,以确保不会出现细微或轻微的遗漏/错误,导致EF无法正确加载/连接。

I was getting the same error.我遇到了同样的错误。 My connection string was as following我的连接字符串如下

 <add name="EmployeeDbContext" connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Sample;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I changed it to following and it fixed the problem我将其更改为关注并解决了问题

<add name="EmployeeDbContext" connectionString="server=.; database=Sample; integrated security=true;"
       providerName="System.Data.SqlClient"/>

Also, be aware when moving the edmx file from say a web application to a data layer, (web.config - connection / app.config - connection) that you change the connectionstring in the data layer AND the webservice layer.此外,当将 edmx 文件从 Web 应用程序移动到数据层时,请注意(web.config - connection / app.config - connection),您更改数据层和 web 服务层中的连接字符串。 (else one uses two different connection strings, and this causes such a problem) (另外一个使用两个不同的连接字符串,这会导致这样的问题)

The connection string "metadata=res" should be same as Edmx name.连接字符串“metadata=res”应与 Edmx 名称相同。 eg例如

metadata=res://*/EmployeeModel.csdl 

this EDMX name should be EmployeeModel这个 EDMX 名称应该是EmployeeModel

I hit this issue and it has bit me many, many, many times and I always forget how I fix it.我遇到了这个问题,它咬了我很多很多次,我总是忘记我是如何解决它的。

All I had to do is set the default.aspx as the start page.我所要做的就是将 default.aspx 设置为起始页。

Obviously this won't help everyone but in my case I have gone down the road of checking my config, connection string...显然这不会对每个人都有帮助,但就我而言,我已经开始检查我的配置、连接字符串......

Hope this help someone, or at least the next time I google it I will find my fix.希望这对某人有所帮助,或者至少下次我用谷歌搜索时我会找到我的修复方法。 :) :)

I have used EF6 in my visual studio 2013. I have solved this by doing the following steps.我在 Visual Studio 2013 中使用了 EF6。我通过执行以下步骤解决了这个问题。

  • Right click on the Entity model右键单击实体模型
  • Open with XML(Text) editor使用 XML(Text) 编辑器打开
  • in tag set ProviderManifestToken="2008"在标签集ProviderManifestToken="2008"
  • build solution构建解决方案

Hope it works for you..希望这对你有用..

I am sharing my code with another developer and he had this problem.No connection string differences.我正在与另一位开发人员共享我的代码,他遇到了这个问题。没有连接字符串差异。 All the files OK... I fixed it by setting所有文件都OK...我通过设置修复了它

  • Metadata Artifact Processing from: "Embed in Output Assembly" to "Copy to output directory" ,元数据工件处理从:“嵌入输出程序集”到“复制到输出目录”,
  • rebuilding the project,重建项目,
  • and then setting it back again.然后再设置回来。

This worked, I assume, because the individual files (CSDL/MSL/SSDL files) were created in the output directory when it used "copy to output directory".我认为这是有效的,因为在使用“复制到输出目录”时在输出目录中创建了单个文件(CSDL/MSL/SSDL 文件)。 Apparently there was a bug that required them for the embedded version to work.显然,有一个错误需要它们才能使嵌入式版本工作。 Hope this helps :)希望这可以帮助 :)

I've hit this several times when moving between development machines after pulling down code from source control.从源代码管理中提取代码后,在开发机器之间移动时,我已经多次遇到这种情况。 I usually just have to explicitly rebuild the project with the EDMX file in it.我通常只需要显式地重建包含 EDMX 文件的项目。

I want to share my experience on this and how I solved it .我想分享我在这方面的经验以及我是如何解决它的 In my case it happens because I copy and pasted my connection string on production server .就我而言,这是因为我连接字符串复制并粘贴生产服务器上 My application was using Entity framework .我的应用程序使用实体框架

Problem was on metadata问题出在元数据上

I named my entity model as 'BetModel' but on my connection string I was using 'Entity' - 'res:// /Entity.csdl|res:// /Entity.ssdl|res://*/Entity.msl' on 'metadata' (reason was because i copy pasted it).我将我的实体模型命名为“BetModel”,但在我的连接字符串中,我使用的是“实体”-“res: ///Entity.csdl |res: ///Entity.ssdl |res://*/Entity.msl”关于“元数据”(原因是因为我复制粘贴了它)。

So my wrong ConnectionString was:所以我错误的 ConnectionString 是:

connectionString="metadata=res://*/Entity.csdl|res://*/Entity.ssdl|res://*/Entity.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=;initial catalog=;persist security info=True;user id=;password=;MultipleActiveResultSets=True;App=EntityFramework&quot;"

and the corrected ConnectionString was:并且更正的 ConnectionString 是:

<add name="BetEntities" connectionString="metadata=res://*/BetModel.csdl|res://*/BetModel.ssdl|res://*/BetModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=;initial catalog=;persist security info=True;user id=;password=;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

If you're trying to use more than one EDMX file and you have to put your new EDMX file in it's own namespace due to conflicts, when creating the EDMX file take note of the metadata the wizard shows you.如果您尝试使用多个 EDMX 文件,并且由于冲突而不得不将新的 EDMX 文件放在它自己的命名空间中,那么在创建 EDMX 文件时,请注意向导显示的元数据。 That one is the correct one.那一个是正确的。

Then later if you are using EntityConnectionStringBuilder and are setting the Metadata there make sure you take into account the new namespace.然后,如果您使用 EntityConnectionStringBuilder 并在那里设置元数据,请确保考虑新的命名空间。

None of the answers here helped me as I had: /EDMX1 /Namespace/EDMX2这里的所有答案都没有像我一样帮助我:/EDMX1/Namespace/EDMX2

and while the metadata for the EDMX1 is "edmx1.csdl|edmx1.ssdl|edmx1.msl" The metadata for EDMX2 is " Namespace.edmx2.csdl ..."而 EDMX1 的元数据是“edmx1.csdl|edmx1.ssdl|edmx1.msl”,而 EDMX2 的元数据是“ Namespace.edmx2.csdl ...”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM