asp.net-mvc 无法加载指定的元数据资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14648212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:34:24  来源:igfitidea点击:

Unable to load the specified metadata resource

asp.net-mvcentity-framework

提问by TheWebGuy

I am having some serious problems with Entity Framework and I can't seem to figure out whats going on.

我在使用 Entity Framework 时遇到了一些严重的问题,我似乎无法弄清楚发生了什么。

I tried many options provided on: MetadataException: Unable to load the specified metadata resourceand the famous Craig Stuntz's blog posting located at: http://blogs.teamb.com/craigstuntz/2010/08/13/38628/

我尝试了以下提供的许多选项:MetadataException:无法加载指定的元数据资源和著名的 Craig Stuntz 的博客文章位于:http: //blogs.teamb.com/craigstuntz/2010/08/13/38628/

I have 3 projects for the sake of brevity:

为简洁起见,我有 3 个项目:

Funscribe.Data (EDMX file located here)

Funscribe.Data(EDMX 文件位于此处)

Funscribe.Console (Console app)

Funscribe.Console(控制台应用程序)

Funscribe.Web (MVC 3 app)

Funscribe.Web(MVC 3 应用程序)

Originally it was just the MVC app and I recently added this new console project.

最初它只是 MVC 应用程序,我最近添加了这个新的控制台项目。

I copied the connection string from web.config and applied it to my app.config:

我从 web.config 复制了连接字符串并将其应用于我的 app.config:

<add name="FundirectoryEntities" connectionString="metadata=res://*/Fundirectory.csdl|res://*/Fundirectory.ssdl|res://*/Fundirectory.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\sqlexpress;initial catalog=Funscribe;user id=sys_Funscribe;password=blah;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

My mvc app continues to work, but when I run the console app, i get the dreaded:

我的 mvc 应用程序继续工作,但是当我运行控制台应用程序时,我感到害怕:

"Unable to load the specified metadata resource."

“无法加载指定的元数据资源。”

I tried changing the connection string to the wild card setting:

我尝试将连接字符串更改为通配符设置:

<add name="FundirectoryEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\sqlexpress;initial catalog=Funscribe;user id=sys_Funscribe;password=blah;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I get: The specified default EntityContainer name 'FundirectoryEntities' could not be found in the mapping and metadata information.

我得到: 在映射和元数​​据信息中找不到指定的默认 EntityContainer 名称“FundirectoryEntities”。

I tried changing it to specify the assembly:

我尝试更改它以指定程序集:

<add name="FundirectoryEntities" connectionString="metadata=res://Funscribe.Data.dll/Fundirectory.csdl|res://Funscribe.Data.dll/Fundirectory.ssdl|res://Funscribe.Data.dll/Fundirectory.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\sqlexpress;initial catalog=Funscribe;user id=sys_Funscribe;password=blah;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I get: Unable to resolve assembly 'Funscribe.Data.dll'.

我得到: 无法解析程序集“Funscribe.Data.dll”。

I am lost on what I should do here. I just upgraded this project to Visual Studio 2012 (I introduce this new console app after converting to VS 2012).

我不知道我应该在这里做什么。我刚刚将这个项目升级到 Visual Studio 2012(我在转换到 VS 2012 后引入了这个新的控制台应用程序)。

I also noticed that the Funscribe.Data.dll is located within the console project's bin folder, i manually deleted these files and watch it get recreated. And what puzzles me is the web app continues to work properly!

我还注意到 Funscribe.Data.dll 位于控制台项目的 bin 文件夹中,我手动删除了这些文件并观察它被重新创建。令我困惑的是网络应用程序继续正常工作

All projects are on .NET 4.0. The console app uses .NET 4.0 not the client profile verison.

所有项目都在 .NET 4.0 上。控制台应用程序使用 .NET 4.0 而不是客户端配置文件版本。

Any help on this matter is greatly appreciated.

非常感谢有关此事的任何帮助。

回答by Sampath

The metadata parameter for an application with an Entity Framework model called Model.edmxin an assembly called Simple Mvc.Data.dllmight look like this:

具有Model.edmx在名为 Simple 的程序集中调用的实体框架模型的应用程序的元数据参数Mvc.Data.dll可能如下所示:

<connectionStrings>
    <add name="MyEntities" connectionString="metadata=
            res://Simple Mvc.Data.dll/Model.csdl|
            res://Simple Mvc.Data.dll/Model.ssdl|
            res://Simple Mvc.Data.dll/Model.msl;provider= <!-- ... -->

So you can see there is one reference for each of the three parts of the EDMX that we need at runtime. They all work in the same way, so let's examine just the first more closely. The CSDL reference looks like this:

所以你可以看到我们在运行时需要的 EDMX 的三个部分中的每一个都有一个参考。它们都以相同的方式工作,所以让我们更仔细地检查第一个。CSDL 参考如下所示:

        res://Simple Mvc.Data.dll/Model.csdl

It specifies three things:

它指定了三件事:

  • We're loading the CSDL from a resource. That's the "res://" part.

  • The name of the assembly which contains the resource, "Simple Mvc.Data.dll". If your assembly is strong named, you can specify a strong name, in all its verbose glory, here.

  • The name of the resource itself, "Model.csdl". Do not confuse this with the EDMX or model name. In this case they happen to be the same, except for the extension, but that's not always true!

  • 我们正在从资源加载 CSDL。那就是"res://" part.

  • 包含资源的程序集的名称,"Simple Mvc.Data.dll". 如果你的程序集是强命名的,你可以在这里指定一个强名称,在它的所有冗长荣耀中。

  • 资源本身的名称,"Model.csdl". 不要将其与 EDMX 或型号名称混淆。在这种情况下,它们碰巧是相同的,除了扩展名,但这并不总是正确的!

It will probably fail if your resources don't happen to have the same name as your model, or if the assembly doesn't happen to be loaded.

如果您的资源与模型的名称不同,或者程序集没有被加载,它可能会失败。

For more information check this out Troubleshooting Entity Framework Connection Strings

有关更多信息,请查看实体框架连接字符串故障排除

I hope this will help to you.

我希望这会对你有所帮助。

回答by oneNiceFriend

Rebuilding solution in Visual Studio helped me.

在 Visual Studio 中重建解决方案帮助了我。

It is on Build -> Rebuild

它在 Build -> Rebuild 上

回答by liss

In my case, the connectionString is trying to find the resources files but unable to. What I did to solve my problem is:

就我而言, connectionString 试图找到资源文件但无法找到。我为解决我的问题所做的是:

Right-Click EDMX diagram -> Go to properties -> Connection -> Metadata Artifact Processing -> Then change to Copy to Output Directoryand rebuild.

右键单击 EDMX 图 -> 转到属性 -> 连接 -> 元数据工件处理 -> 然后更改为复制到输出目录并重建。

This will change your metadata connectionstring path point to the resources he's trying to find.

这将更改您的元数据连接字符串路径指向他试图查找的资源。

回答by Penny

in my case, I delete my EDMX file in project and create it again with a new name, eg. before it was Home_Model, later it is Home_Cost. when I try to invoke the below code, it has "Unable to load the specified metadata resource." error. /////

就我而言,我删除了项目中的 EDMX 文件并使用新名称重新创建它,例如。之前是 Home_Model,之后是 Home_Cost。当我尝试调用以下代码时,它显示“无法加载指定的元数据资源”。错误。/////

    using (var currentEntities = new masterEntities())
    {
        currentUser = currentEntities.Home_User.FirstOrDefault(
            x => x.UserLoginAccount == userLoginAccount && x.UserPwd == userPwd);

    }

//// so in Web.config, I modify the metadata configuation from Home_Model to Home_Cost.

//// 所以在 Web.config 中,我将元数据配置从 Home_Model 修改为 Home_Cost。

then it works. obviously, VS2015(my version) didn't update web.config when I delete/create edmx.

那么它的工作原理。显然,当我删除/创建 edmx 时,VS2015(我的版本)没有更新 web.config。

回答by ravi saini

MetadataException: Unable to load the specified metadata resource

MetadataException: 无法加载指定的元数据资源

This means that the application is unable to load the EDMX.

这意味着应用程序无法加载 EDMX。

There are several things which can cause this, but most likely, due to invalid connection string.

有多种原因可能导致这种情况,但最有可能的原因是连接字符串无效。

Possible Solutions:

可能的解决方案:

1.Check connection string 2.Refresh model.edmx 3.Select option "Update Model from database" in model.edmx

1.检查连接字符串 2.刷新model.edmx 3.在model.edmx中选择“从数据库更新模型”选项

http://mvc4asp.blogspot.in/

http://mvc4asp.blogspot.in/