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

Mad*_*Boy 6 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;
    }
Run Code Online (Sandbox Code Playgroud)

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();
    }
Run Code Online (Sandbox Code Playgroud)

如果我在我的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"

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

Mad*_*Boy 27

看完这篇答案文章和这篇博客后,我改变了:

  entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl";
Run Code Online (Sandbox Code Playgroud)

至:

  entity.Metadata = "res://*/";
Run Code Online (Sandbox Code Playgroud)

它的工作原理:-)

  • 抛出 ArgumentException:参数“xmlReader”无效。必须至少提供一个 .ssdl 工件。 (2认同)