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

有人有什么想法吗?

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

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


当前回答

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

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

其他回答

经过数小时的谷歌搜索和尝试解决建议的解决方案没有一个有效。我在这里列出了几个解决方案。我也注意到了对我有用的方法。(我使用的是EF版本6.1.1,SQL server 2014 -但旧的DB)

重新构建项目并重试。 关闭和打开VS -我不知道这是怎么回事 确保如果你已经把。edmx文件放在一个目录中,确保你在你的连接字符串中包含了目录。例如,我的是在DAL文件夹。所以它看起来是这样的:connectionString="metadata=res://*/ dal . nammodel .csdl|res://*/ dal . nammodel .ssdl|res://*/ dal . nammodel .msl;你可以在解决方案资源管理器中切换显示所有文件,在~/obj/..目录)

...以及更多我尝试过的方法[比如:将EntityFramework版本恢复到后来的版本(不确定)]


对我有用的是:

这篇文章帮助我解决了我的问题。我只是在EDMX文件中将我的ProviderManifestToken=“2012”更改为ProviderManifestToken=“2008”。这样做:

解决方案资源管理器

右键单击文件。edmx 开放与. . XML编辑器 使用2008更改ProviderManifestToken="XXXX"

我希望这对你有所帮助。

当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文件的文件名。当资源与元数据值中指定的资源具有不同的名称时,将无法正常工作。

有时我在我的项目中看到这个错误。我通过

1 -右键单击EDMX文件

2 -选择“运行自定义工具”选项

3 -重建项目

我编写了这个helper类,当ObjectContext对象定义在与使用它的项目不同的项目中时,它可以创建对象的实例。我解析配置文件中的连接字符串,并将'*'替换为完整的程序集名称。

它并不完美,因为它使用反射来构建对象,但这是我能找到的最通用的方法。

希望它能帮助到别人。

public static class EntityHelper<T> where T : ObjectContext
{
    public static T CreateInstance()
    {
        // get the connection string from config file
        string connectionString = ConfigurationManager.ConnectionStrings[typeof(T).Name].ConnectionString;

        // parse the connection string
        var csBuilder = new EntityConnectionStringBuilder(connectionString);

        // replace * by the full name of the containing assembly
        csBuilder.Metadata = csBuilder.Metadata.Replace(
            "res://*/",
            string.Format("res://{0}/", typeof(T).Assembly.FullName));

        // return the object
        return Activator.CreateInstance(typeof(T), csBuilder.ToString()) as T;
    }
}

对我来说,我把数据访问层和用户界面层分开了。 每一层都有实体连接字符串。

在我将这两个分离的连接字符串修改为相同之前,我仍然发现下面的错误。

Unable to load the specified metadata resource

所以我让这两个层(DAL, UI)是相同的连接字符串,它的工作完美。

我的解决方案是使所有的连接字符串是相同的,无论他们已经提出。