我决定将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,它将引发另一个异常
"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"
我知道我缺少一些必须更改的内容,以便自行生成的代码使用新的连接字符串,但在哪里可以找到它?

最佳答案

阅读this answers文章和this blog之后,我进行了更改:

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

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

它有效:-)

09-10 11:50