c# - Entity Framework : Unable to load the specified metadata resource

标签 c# entity-framework

我决定将 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;
    }

我在 .Designer.cs 中抛出异常 Unable to load the specified metadata resource.

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

如果我在实体创建器中定义 .Name,它会抛出另一个异常

“指定‘名称’关键字时不允许使用其他关键字。” (System.ArgumentException) Exception Message = "Other keywords are not allowed when the 'Name' keyword is specified.",异常类型 = "System.ArgumentException"

我知道我遗漏了一些必须更改的内容,以便自行生成的代码使用新的连接字符串,但在哪里寻找它?

最佳答案

看完this answers文章和this blog我改变了:

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

收件人:

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

而且有效:-)

关于c# - Entity Framework : Unable to load the specified metadata resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971698/

相关文章:

c# - 连接两个表以使用嵌套集合填充 viewModel

asp.net-mvc - 如何从 Entity Framework 模型中删除对象而不先加载它?

c# - 这个棕褐色调转换算法有什么问题?

c# - Ruby 的以下行做了什么? "integer >> integer"

c# - Azure Function ServiceBusReceivedMessage ApplicatonProperties 为空

c# - 通过访问数据库来测试 Entity Framework 数据

.net - 当业务规则是属性可访问性时,如何测试它?

c# - 在运行时使用反射构造泛型的具体类型的实例

c# - 许多自定义用户控件实现相同的界面;有没有办法避免在每个代码中重复相同的代码?

entity-framework - 强制 OData Controller 将更新的实体返回给客户端