C# MetadataException: 无法加载指定的元数据资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/689355/
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
MetadataException: Unable to load the specified metadata resource
提问by J. Steen
All of a sudden I keep getting a MetadataException
on instantiating my generated ObjectContext
class. The connection string in App.Config looks correct - hasn't changed since last it worked - and I've tried regenerating a new model (edmx-file) from the underlying database with no change.
突然间,我一直MetadataException
对实例化我生成的ObjectContext
类感到厌烦。App.Config 中的连接字符串看起来正确 - 自上次工作以来没有改变 - 我已经尝试从底层数据库重新生成一个新模型(edmx-file)而没有任何变化。
Anyone have any ideas?
谁有想法?
Further details: I haven't changed any properties, I haven't changed the name of any output assemblies, I haven't tried to embed the EDMX in the assembly. I've merely waited 10 hours from leaving work until I got back. And then it wasn't working anymore.
更多详细信息:我没有更改任何属性,我没有更改任何输出程序集的名称,我没有尝试在程序集中嵌入 EDMX。我从下班到回来只等了 10 个小时。然后它不再起作用了。
I've tried recreating the EDMX. I've tried recreating the project. I've even tried recreating the database, from scratch. No luck, whatsoever.
我试过重新创建 EDMX。我试过重新创建该项目。我什至尝试从头开始重新创建数据库。没有运气,无论如何。
采纳答案by Craig Stuntz
This means that the application is unable to load the EDMX. There are several things which can cause this.
这意味着应用程序无法加载 EDMX。有几种情况会导致这种情况。
- You might have changed the MetadataArtifactProcessing property of the model to Copy to Output Directory.
- The connection string could be wrong. I know you say you haven't changed it, but if you have changed other things (say, the name of an assembly), it could still be wrong.
- You might be using a post-compile task to embed the EDMX in the assembly, which is no longer working for some reason.
- 您可能已将模型的 MetadataArtifactProcessing 属性更改为复制到输出目录。
- 连接字符串可能有误。我知道你说你没有改变它,但如果你改变了其他东西(比如程序集的名称),它仍然可能是错误的。
- 您可能正在使用编译后任务将 EDMX 嵌入到程序集中,但由于某种原因不再工作。
In short, there is not really enough detail in your question to give an accurate answer, but hopefully these ideas should get you on the right track.
简而言之,您的问题中没有足够的细节来给出准确的答案,但希望这些想法能让您走上正轨。
Update:I've written a blog post with more complete steps for troubleshooting.
更新:我写了一篇博客文章,其中包含更完整的故障排除步骤。
回答by J. Steen
The ultimate solution (even after recreating the database on two other machines, as well as the EDMX and other sundries) was to not use the first edition of Entity Framework. Looking forward to evaluating it again in .NET 4.0.
最终的解决方案(即使在另外两台机器上重新创建数据库,以及 EDMX 和其他杂物)是不使用实体框架的第一版。期待在 .NET 4.0 中再次评估它。
After running into the same problem againand searching all over for an answer, I finally found someone who'd had the same problem. It appears that the connection string wasn't correctly generated by Visual Studio's wizard, and the link to the metadata resources was missing an important path.
再次遇到同样的问题并四处寻找答案后,我终于找到了遇到同样问题的人。连接字符串似乎没有由 Visual Studio 的向导正确生成,并且元数据资源的链接缺少重要路径。
v1.0 BUG?: Unable to load the specified metadata resource. Scripts != Models
v1.0 BUG?: 无法加载指定的元数据资源。脚本 != 模型
Update 2013-01-16: Having transitioned to almost exclusively using EF Code First practices (even with existing databases) this problem is no longer an issue. For me, that was a viable solution to reducing the clutter from auto-generated code and configuration and increasing my own control over the product.
2013年 1 月 16 日更新:已过渡到几乎完全使用 EF Code First 实践(即使使用现有数据库),此问题不再是问题。对我来说,这是一个可行的解决方案,可以减少自动生成的代码和配置带来的混乱,并增加我自己对产品的控制。
回答by MicTech
This little change help with this problem.
这个小小的改变有助于解决这个问题。
I have Solution with 3 project.
我有 3 个项目的解决方案。
connectionString="metadata=res://*/Model.Project.csdl|res://*/Model.Project.ssdl|res://*/Model.Project.msl;
change to
改成
connectionString="metadata=res://*/;
回答by Pitming
I had the same problem. I looked into my complied dll with reflector and have seen that the name of the resource was not right. I renamed and it looks fine now.
我有同样的问题。我用反射器查看了我编译的 dll,发现资源的名称不正确。我重命名了,现在看起来不错。
回答by Rick Arthur
I had a similar error. I had recreated the project (long story), and pulled everything over from the old project. I hadn't realized that my model had been in a directory called 'Model' before, and was now in a directory called 'Models'. Once I changed the connection in my Web.Config from this:
我有一个类似的错误。我重新创建了这个项目(长篇故事),并从旧项目中提取了所有内容。我没有意识到我的模型之前一直在名为“Model”的目录中,而现在位于名为“Models”的目录中。一旦我改变了我的 Web.Config 中的连接:
<add name="RecipeManagerEntities" connectionString="metadata=res://*/Model.Recipe.csdl
to this:
对此:
<add name="RecipeManagerEntities" connectionString="metadata=res://*/Models.Recipe.csdl
Everything worked (changed Model
to Models
). Note that I had to change this three places in this string.
一切正常(更改Model
为Models
)。请注意,我必须更改此字符串中的这三个位置。
回答by hgcummings
This happened to me when I accidentally switched the Build Action of the edmx file (appears under Properties in the IDE) from 'EntityDeploy' to 'None'. EntityDeploy is what populates the metadata for you: see http://msdn.microsoft.com/en-us/library/cc982037.aspx
当我不小心将 edmx 文件的构建操作(出现在 IDE 中的“属性”下)从“EntityDeploy”切换到“None”时,发生了这种情况。EntityDeploy 是为您填充元数据的内容:请参阅http://msdn.microsoft.com/en-us/library/cc982037.aspx
回答by TobyEvans
I've just spent a happy 30 minutes with this. I'd renamed the entities object, renamed the entry in the config file, but there's more ... you have to change the reference to the csdl as well
我刚刚度过了愉快的 30 分钟。我重命名了实体对象,重命名了配置文件中的条目,但还有更多……您还必须更改对 csdl 的引用
very easy to miss - if you're renaming, make sure you get everything....
很容易错过-如果您要重命名,请确保获得所有内容....
回答by eagle779
I also had the same problem and solution as per Rick, except that I was importing an existing .edmx to a new project, and while the base namespace didn't matter it was imported into a different subdirectory so I also had to update the connection string inside Web.Config in three places, to include the different subdirectory naming:
我也遇到了与 Rick 相同的问题和解决方案,除了我将现有的 .edmx 导入到新项目中,虽然基本命名空间无关紧要,但它已导入到不同的子目录中,因此我还必须更新连接字符串在 Web.Config 中的三个位置,以包含不同的子目录命名:
回答by alpav
My theory is that if you have more than one edmx file with same name (Model1 for example), it will give that exception. I've got same problem when I decided to name all my edmx files (sitting in different projects) as Model1 because I thought they should be independent.
我的理论是,如果您有多个同名的 edmx 文件(例如 Model1),它会给出该异常。当我决定将所有 edmx 文件(位于不同项目中)命名为 Model1 时,我遇到了同样的问题,因为我认为它们应该是独立的。
回答by user276695
You can get this exception when the Edmx is in one project and you are using it from another.
当 Edmx 在一个项目中并且您正在另一个项目中使用它时,您可能会收到此异常。
The reason is Res://*/
is a uri which points to resources in the CURRENT assembly. If the Edm is defined in a different assembly from the code which is using it, res://*/ is not going to work because the resource cannot be found.
原因是Res://*/
一个 uri 指向 CURRENT 程序集中的资源。如果 Edm 是在与使用它的代码不同的程序集中定义的,则 res://*/ 将无法工作,因为找不到资源。
Instead of specifying ‘*', you need to provide the full name of the assembly instead (including public key token). Eg:
您需要提供程序集的全名(包括公钥标记),而不是指定“*”。例如:
res://YourDataAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdefabcedf/YourEdmxFileName.csdl|res://...
A better way to construct connection strings is with EntityConnectionStringBuilder:
构造连接字符串的更好方法是使用 EntityConnectionStringBuilder:
public static string GetSqlCeConnectionString(string fileName)
{
var csBuilder = new EntityConnectionStringBuilder();
csBuilder.Provider = "System.Data.SqlServerCe.3.5";
csBuilder.ProviderConnectionString = string.Format("Data Source={0};", fileName);
csBuilder.Metadata = string.Format("res://{0}/YourEdmxFileName.csdl|res://{0}/YourEdmxFileName.ssdl|res://{0}/YourEdmxFileName.msl",
typeof(YourObjectContextType).Assembly.FullName);
return csBuilder.ToString();
}
public static string GetSqlConnectionString(string serverName, string databaseName)
{
SqlConnectionStringBuilder providerCs = new SqlConnectionStringBuilder();
providerCs.DataSource = serverName;
providerCs.InitialCatalog = databaseName;
providerCs.IntegratedSecurity = true;
var csBuilder = new EntityConnectionStringBuilder();
csBuilder.Provider = "System.Data.SqlClient";
csBuilder.ProviderConnectionString = providerCs.ToString();
csBuilder.Metadata = string.Format("res://{0}/YourEdmxFileName.csdl|res://{0}/YourEdmxFileName.ssdl|res://{0}/YourEdmxFileName.msl",
typeof(YourObjectContextType).Assembly.FullName);
return csBuilder.ToString();
}
If you still encounter the exception, open the assembly in reflector and check the filenames for your .csdl, .ssdl and .msl files. When the resources have different names to the ones specified in the metadata value, it's not going to work.
如果仍然遇到异常,请在反射器中打开程序集并检查 .csdl、.ssdl 和 .msl 文件的文件名。当资源的名称与元数据值中指定的名称不同时,它将不起作用。