首页 > 解决方案 > 为什么在代码中使用 EF6 connectionString 时出现“System.Data.MetadataException:'无法加载指定的元数据资源。'”错误?

问题描述

我知道有许多与此错误有关的问题/答案,我很抱歉重复,但到目前为止我还没有看到任何处理尝试 Open() 实体连接时程序代码发生的错误.

我有一个VS2017 C# Windows Forms解决方案,.NET 4.5.2使用WCF服务应用程序 Entity Framework 6.2,包括逻辑层、BDO 层和数据层。我的实体上下文已创建database-first

我的服务self-hosted在我的 UI 客户端项目的Program.cs文件中。传输是net.pipe因为客户端和服务都在同一台计算机上。

我的数据库是远程的 version ,需要用户 ID 和密码,并且它的连接字符串在为我的 UI 客户端项目SQL Server 2016 (130)定义时效果很好。App.config

但是,出于安全目的,我目前正在尝试从该方法App.config中删除连接字符串并将其编码。Program.cs Main()

我正在使用SqlConnectionStringBuilderandEntityConnectionStringBuilder来构建实体连接字符串“provider”和“metadata”片段,并且我可以在我的Program.cs代码中构建与在App.config.

但是,当我的代码打开实体连接时,出现错误:

System.Data.MetadataException:Unable to load the specified metadata resource.

这是我的App.config connectionString部分,效果很好:

<connectionStrings>
    <add name="MyDBEntities" 
         connectionString="metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=myremotewebsite.com;initial catalog=MyDB;persist security info=True;user id=theUserID;password=thePassword;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

这是我Program.cs在 UI 客户端项目中的 C# 代码:

using System.ServiceModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.EntityClient;

namespace myUICLient
{
    static class Program
    {
        public static ServiceHost hostMyService;

        // Specify the provider name, server and database.
        public static string sqlProvider = "System.Data.SqlClient"; 
        public static string serverName = "myremotewebsite.com";
        public static string databaseName = "MyDB";
        public static string userID = "theUserID";
        public static string passWord = "thePassword";
        public static string App = "EntityFramework";
        public static string providerString;
    
        public static SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        public static EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();

        public static EntityConnection MyDBEntities;   

        [STAThread]
        static void Main()
        {
            hostMyService = new ServiceHost(typeof(prjMyServiceApplication.MyService),
                                   new Uri("net.pipe://localhost/prjUICLient/MyService.svc"));
            hostMyService.Open();

            // Set the properties for the data source.
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            sqlBuilder.UserID = userID;
            sqlBuilder.Password = passWord;
            sqlBuilder.IntegratedSecurity = false;
            sqlBuilder.PersistSecurityInfo = true;
            sqlBuilder.MultipleActiveResultSets = true;
            sqlBuilder.ApplicationName = App;

            // Build the SqlConnection connection string.
            providerString = sqlBuilder.ToString();

            // Set the provider name.
            entityBuilder.Provider = sqlProvider;  
        
            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl";

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;

            using (MyDBEntities = new EntityConnection(entityBuilder.ToString()))
            {
                MyDBEntities.Open(); // gets the error here!

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new myWinForm());

                hostMyService.Close();

                MyDBEntities.Close();
           }
       }
   }
}

当代码出错时,我可以在错误数据中查看连接字符串的元数据部分,还可以看到“服务器版本”错误:

       "metadata=res://*/MyDB.csdl|
        res://*/MyDB.ssdl|
        res://*/MyDB.msl;
        provider=System.Data.SqlClient;provider connection string=\"Data Source=myremotewebsite.com;
        Initial Catalog=MyDB;Integrated Security=False;Persist Security Info=True;
        User ID=MyUserID;Password=MyPassword;
        MultipleActiveResultSets=True;Application Name=EntityFramework\""

MyDBEntities.ServerVersion = 'MyDBEntities.ServerVersion' 引发了“System.InvalidOperationException”类型的异常

当我注释掉 Main() 中的 Open() 语句并再次运行时,在进行 Service 调用时,我从 DAL 层收到以下错误:

基础提供程序在打开时失败

感谢您提供的任何帮助或建议。

Update 2021.03.04在此路径下找到我的数据层项目folder location中的文件后:model

prjDAL
。对象。发布 。edmxResourcesToEmbed 。MyDB.csdl、MyDB.msl、MyDB.ssdl

changed my metadata string对以下内容:

      entityBuilder.Metadata = @"res://prjDAL.dll/MyDB.csdl|
                                 res://prjDAL.dll/MyDB.ssdl|
                                 res://prjDAL.dll/MyDB.msl";

然后重新运行,得到了这个new error:

System.IO.FileNotFoundException:'无法解析程序集prjDAL.dll

我已经阅读了推荐链接中的文章,到目前为止我学到的是“资源”和“模型”是两个不同的东西。链接文章中的示例类似于 MVC 示例,其中连接字符串位于 App.config 文件中。我没有使用 MVC,而是使用 .NET 4.5.2 n-Tier winforms 设计和I don't have a problem when I use the connection string in the App.config文件。我的问题出现在尝试创建连接字符串in code并尝试open the EntityConnection反对时。

以下是我尝试过的更多选项:此元数据:

  entityBuilder.Metadata = @"res://prjDAL.dll/resources.MyDB.csdl|
                             res://prjDAL.dll/resources.MyDB.ssdl|
                             res://prjDAL.dll/resources.MyDB.msl";

得到这个错误:

System.IO.FileNotFoundException:Unable to resolve assembly 'prjDAL.dll'.

而这个元数据:

  entityBuilder.Metadata=
      @"res://prjUICLient/bin/prjDAL.dll/
              prjUICLient.Resources.MyDB.csdl|

        res://prjUICLient/bin/prjDAL.dll/
              prjUICLient.Resources.MyDB.ssdl|

        res://prjUICLient/bin/prjDAL.dll/
              prjUICLient.Resources.MyDB.msl";

得到这个错误:

System.Data.MetadataException:The specified metadata path is not valid. 有效路径必须是现有目录、扩展名为“.csdl”、“.ssdl”或“.msl”的现有文件,或者是标识嵌入资源的 URI。

再次尝试将元数据指向 DAL 层中的程序集:

  entityBuilder.Metadata = @"res://prjDAL/obj/Release/prjDAL.dll/
                           prjDAL.obj.release.edmxResourcesToEmbed.MyDB.csdl|
                res://prjDAL/obj/Release/prjDAL.dll/
                           prjDAL.obj.release.edmxResourcesToEmbed.MyDB.ssdl|
                res://prjDAL/obj/Release/prjDAL.dll/                                                   
                           prjDAL.obj.release.edmxResourcesToEmbed.MyDB.msl";

System.Data.MetadataException:The specified metadata path is not valid. 有效路径必须是现有目录、扩展名为“.csdl”、“.ssdl”或“.msl”的现有文件,或者是标识嵌入资源的 URI。

有没有人将这个实体连接字符串从 App.config 转换为 .NET 4.5.2 WinForms 多层应用程序中的代码?

Do I need to upgrade my application to .NET Standard or .NET CORE?

标签: sql-serverwcfentity-framework-6

解决方案


推荐阅读