C# 实体框架:无法加载指定的元数据资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8971698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:24:05  来源:igfitidea点击:

Entity Framework: Unable to load the specified metadata resource

c#entity-framework

提问by MadBoy

I decided to move Entity Connection Stringfrom app.configto code. However after setting it up like this:

我决定Entity Connection Stringapp.config代码开始。但是这样设置后:

    public static string GetConnectionString() {
        string connection = "";

        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder.DataSource = dbServer;
        sqlBuilder.InitialCatalog = dbInitialCatalog;

        sqlBuilder.IntegratedSecurity = false;
        sqlBuilder.UserID = dbUserName;
        sqlBuilder.Password = dbPasswWord;
        sqlBuilder.MultipleActiveResultSets = true;

        EntityConnectionStringBuilder entity = new EntityConnectionStringBuilder();
       // entity.Name = "EntityBazaCRM";
        entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl";

        entity.Provider = "System.Data.SqlClient";
        entity.ProviderConnectionString = sqlBuilder.ToString();

        connection = entity.ToString();

        return connection;
    }

I have an exception thrown Unable to load the specified metadata resource.in .Designer.cs.

Unable to load the specified metadata resource.在 .Designer.cs 中抛出了一个异常。

    /// <summary>
    /// Initialize a new EntityBazaCRM object.
    /// </summary>
    public EntityBazaCRM(string connectionString) : base(connectionString, "EntityBazaCRM")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

If I define .Name inside my Entity creator it throws another exception

如果我在我的实体创建者中定义 .Name 它会引发另一个异常

"Other keywords are not allowed when the 'Name' keyword is specified." (System.ArgumentException) Exception Message = "Other keywords are not allowed when the 'Name' keyword is specified.", Exception Type = "System.ArgumentException"

"Other keywords are not allowed when the 'Name' keyword is specified." (System.ArgumentException) Exception Message = "Other keywords are not allowed when the 'Name' keyword is specified.", Exception Type = "System.ArgumentException"

I know I'm missing something that I have to change so that self generated code uses new connection string but where to look for it?

我知道我错过了一些我必须更改的东西,以便自生成的代码使用新的连接字符串,但在哪里寻找它?

采纳答案by MadBoy

After reading this answersarticle and this blogI changed:

阅读完这篇回答文章和这篇博客后,我改变了:

  entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl";

To:

到:

  entity.Metadata = "res://*/";

And it works :-)

它有效:-)

回答by Thiru

I upgraded to the new csproj format(Visual studio 2017 with simple format) after that I started getting this error. The csproj has a feature where you don't need to include each file instead it includes all the relevant files under the folder by default so the entity framework files are treated same way so those are not embedded into the assembly by default.

之后我开始收到此错误,升级到新的 csproj 格式(Visual Studio 2017 with simple format)。csproj 有一个特性,您不需要包含每个文件,而是默认包含文件夹下的所有相关文件,因此实体框架文件的处理方式相同,因此默认情况下它们不会嵌入到程序集中。

I need to go and manually change the build action of my edml file(edmx in case of Microsoft entity framework) to 'DevartEntityDeploy' (I hope it is EntityDeploy for Microsoft Entity framework)and build it which solved my problem

我需要手动将我的 edml 文件(Microsoft 实体框架中的 edmx)的构建操作更改为“DevartEntityDeploy”(我希望它是 Microsoft Entity 框架的 EntityDeploy)并构建它解决了我的问题

enter image description here

在此处输入图片说明