突然,在实例化生成的ObjectContext类时,我一直得到一个MetadataException。App.Config中的连接字符串看起来是正确的-自从上次它工作以来没有改变-我已经尝试从底层数据库中重新生成一个没有变化的新模型(edmx-file)。

有人有什么想法吗?

进一步的细节:我没有更改任何属性,没有更改任何输出程序集的名称,也没有尝试将EDMX嵌入到程序集中。我只是下班后等了10个小时才回来。然后它就不管用了。

我试过重新创造EDMX。我试着重新创建这个项目。我甚至尝试从头开始重新创建数据库。不管怎样,运气不好。


当前回答

当Edmx在一个项目中,而您正在从另一个项目中使用它时,可以得到此异常。

原因是Res://*/是一个指向当前程序集中资源的uri。如果Edm是在与使用它的代码不同的程序集中定义的,res://*/将不起作用,因为无法找到资源。

您需要提供程序集的全名(包括公钥令牌),而不是指定' * '。例如:

res://YourDataAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdefabcedf/YourEdmxFileName.csdl|res://...

构造连接字符串的更好方法是使用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();
}

如果仍然遇到异常,请在reflector中打开程序集并检查.csdl、.ssdl和.msl文件的文件名。当资源与元数据值中指定的资源具有不同的名称时,将无法正常工作。

其他回答

和一个快速的方法来检查模型名称没有Reflector....查找目录

...obj - {} - edmxResourcesToEmbed输出配置

检查.csdl、.msl和.ssdl资源文件是否存在。如果它们在子目录中,则子目录的名称必须加在模型名称之前。

例如,我的三个资源文件在一个子目录Data中,所以我的连接字符串必须是

元= res: / / * Data.MyModel.csdl | res: / * / Data.MyModel.ssdl | res: / * / Data.MyModel.msl;

(与元= res: / / * / MyModel csdl | res: / / * / MyModel ssdl | res: / / * / MyModel msl;)。

这意味着应用程序无法加载EDMX。有几种情况会导致这种情况。

您可能已经将模型的MetadataArtifactProcessing属性更改为Copy to Output Directory。 连接字符串可能错误。我知道您说您没有更改它,但如果您更改了其他内容(例如,程序集的名称),它仍然可能是错误的。 您可能正在使用编译后任务将EDMX嵌入到程序集中,但由于某种原因该程序集中不再工作。

简而言之,你的问题中没有足够的细节来给出准确的答案,但希望这些想法能让你走上正确的轨道。

更新:我已经写了一篇博客文章,介绍了更完整的故障排除步骤。

只需输入path,而不是{path . to . path。}: res: / / / {Path.To.The。}YourEdmxFileName.csdl | res: / / / {Path.To.The。}YourEdmxFileName.ssdl | res: / / * / {Path.To.The。}YourEdmxFileName.msl

在我的情况下,列出的答案没有一个是有效的,所以我发布了这个。

For my case, building on Visual studio and running it with IIS express worked fine. But when I was deploying using Nant scripts as a stand-alone website was giving errors. I tried all the suggestions above and then realized the DLL that was generated by the nant script was much smaller than the one generated by VS. And then I realized that Nant was unable to find the .csdl, .msl and .ssdl files. So then there are really two ways to solve this issue, one is to copy the needed files after visual studio generates them and include these files in the build deployment. And then in Web.config, specify path as:

"metadata=~/bin/MyDbContext.csdl|~/bin/MyDbContext.ssdl|~/bin/MyDbContext.msl;provider=System.Data.SqlClient;...."

这是假设您已经手动复制文件到您正在运行的网站的bin目录。如果它在不同的目录中,则相应地修改path。 第二种方法是在Nant脚本中执行EdmGen.exe,并生成文件,然后将它们作为资源,如下例所示: https://github.com/qwer/budget/blob/master/nant.build

我也遇到了和Rick一样的问题和解决方案,只是我要将一个现有的.edmx导入到一个新项目中,虽然基本名称空间无关紧要,但它被导入到不同的子目录中,因此我还必须更新Web内部的连接字符串。配置在三个地方,包括不同的子目录命名: