简体   繁体   English

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

[英]Entity Framework: Unable to load the specified metadata resource

I decided to move Entity Connection String from app.config to code. 我决定将Entity Connection Stringapp.config移动到代码。 However after setting it up like this: 但是在设置之后如下:

    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. 我抛出异常Unable to load the specified metadata resource. in .Designer.cs. 在.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 如果我在我的Entity创建者中定义.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"

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? 我知道我错过了一些我必须改变的东西,以便自生成的代码使用新的连接字符串,但在哪里寻找它?

After reading this answers article and this blog I changed: 看完这篇答案文章和这篇博客后,我改变了:

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

To: 至:

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

And it works :-) 它的工作原理:-)

I upgraded to the new csproj format(Visual studio 2017 with simple format) after that I started getting this error. 我升级到新的csproj格式(简单格式的Visual Studio 2017)之后我开始收到此错误。 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具有一个功能,您不需要包含每个文件,而是默认包含该文件夹下的所有相关文件,因此实体框架文件的处理方式相同,因此默认情况下不会嵌入到程序集中。

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)并构建它解决了我的问题

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM