简体   繁体   English

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

[英]MetadataException - Unable to load the specified metadata resource?

I have a solution with two relevant projects.我有两个相关项目的解决方案。 The first builds My.exe , and the second builds a class library MyModel.dll that contains only my EF model.第一个构建My.exe ,第二个构建一个 class 库MyModel.dll ,其中仅包含我的 EF model。

I'm getting a MetadataException in my Model's VS-generated ObjectContext ctor.我在模型的 VS 生成的ObjectContext ctor 中收到MetadataException I've read through Troubleshooting Entity Framework Connection Strings , but I still haven't been able to narrow down my problem.我已通读Troubleshooting Entity Framework Connection Strings ,但仍然无法缩小问题范围。

The offending constructor code:有问题的构造函数代码:

public MyEntities() :
    base(@"name=MyEntities", "MyEntities") // MetadataException here
{
    this.ContextOptions.LazyLoadingEnabled = true;
    OnContextCreated();
}

Metadata Artifact Processing is set to EmbedInOutputAssembly .元数据工件处理设置为EmbedInOutputAssembly When I open MyModel.dll in Reflector, I see:当我在 Reflector 中打开MyModel.dll时,我看到:

  • DataAccessLayer.MyModel.csdl DataAccessLayer.MyModel.csdl
  • DataAccessLayer.MyModel.msl DataAccessLayer.MyModel.msl
  • DataAccessLayer.MyModel.ssdl DataAccessLayer.MyModel.ssdl

I've tried setting Build Action for my app.config to None and Content , and neither makes a difference.我尝试将我的app.configBuild Action设置为NoneContent ,但两者都没有区别。 The config file contains:配置文件包含:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyEntities" 
         connectionString="metadata=
            res://*/DataAccessLayer.MyModel.csdl|
            res://*/DataAccessLayer.MyModel.ssdl|
            res://*/DataAccessLayer.MyModel.msl;
            provider=Devart.Data.PostgreSql;
            provider connection string=&quot;
                User Id=MY_USER;
                Password=MY_PASS;
                Host=127.0.0.1;
                Database=MY_DB;
                Persist Security Info=True&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

I've tried replacing the resource prefix res://*/ with both res://MyModel.dll/ and res://MyModel/ , leaving the rest intact (because it matches the resources in MyModel.dll ), but neither solved the problem.我尝试用res://MyModel.dll/res://MyModel/替换资源前缀res://*/ ,使 rest 完好无损(因为它与MyModel.dll中的资源匹配),但两者都没有解决了这个问题。 My class library is strong-named .我的 class 库是strong-named

Both my executable and class library projects contain references to System.Data.Entity and Devart.Data.PostgreSql , and My.dll is being built to the same location as My.exe .我的可执行文件和 class 库项目都包含对System.Data.EntityDevart.Data.PostgreSql的引用,并且My.dll正在构建到与My.exe相同的位置。

I stepped into .NET source code , and by examining the arguments & local variables of internal EF calls, found the resource assembly name was still set to * .进入了 .NET 源代码,通过检查内部 EF 调用的 arguments 和局部变量,发现资源程序集名称仍然设置为*

It turns out a bug in my application code set the model's default connection string before calling the ObjectContext ctor.结果是我的应用程序代码中的一个错误在调用ObjectContext ctor之前设置了模型的默认连接字符串。 It was pulled from a Settings.settings file, which hadn't been recently sync'd with the latest, correct app.config .它是从Settings.settings文件中提取的,该文件最近没有与最新的正确app.config同步。

Some reflections on our discussion in the comments:评论中对我们讨论的一些思考:

  • The correct assembly name in app.config is MyModel rather than MyModel.dll (as shown in Craig's article.) Using MyModel.dll produces an exception in the EF code. app.config 中正确的程序集名称是MyModel而不是MyModel.dll (如 Craig 的文章中所示。)使用MyModel.dll会在 EF 代码中产生异常。

  • Calling the ctor as base(@"name=MyEntities") instead of base(@"name=MyEntities", "MyEntities") actually produced a new exception in ObjectContext.GetEntitySetName()将 ctor 称为base(@"name=MyEntities")而不是base(@"name=MyEntities", "MyEntities")实际上在ObjectContext.GetEntitySetName()中产生了一个新异常

I updated Settings.settings to be in sync with app.config , and everything is now working.我更新了Settings.settings以与app.config同步,现在一切正常。

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

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