2

我正在尝试连接我的实体框架映射以与托管站点上的 Sql 服务器集成。

此问题可在以下堆栈跟踪中找到:

[MetadataException: Unable to load the specified metadata resource.]
   System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +387
   System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource..ctor(String originalPath, String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +72
   System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +222
   System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +74
   System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry) +68

我假设我的连接字符串没有正确设置,因为我需要在运行时动态生成这个字符串。字符串如下所示:

<add name="Model1Container-providerConnectionString" 
     connectionString="data source=something.sqlserver.sequelizer.com;
     User ID=**; 
     Password=**;
     Initial Catalog=dbd5d3955e11834e1088929f9d005af0a8;
     user instance=True;multipleactiveresultsets=True;App=EntityFramework" />

然后在运行时按如下方式构造字符串:

    public static string GetConnectionString()
    {
        string baseConnectionString = ConfigurationManager.ConnectionStrings["Model1Container-providerConnectionString"].ConnectionString;

        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = baseConnectionString;
        entityBuilder.Metadata = @"res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl";

        return entityBuilder.ToString();
    }
}

几天来一直在研究这个问题,非常感谢社区在寻找工作解决方案方面的专业知识。

4

0 回答 0