【问题标题】:Entity Framework: Unable to load the specified metadata resource实体框架:无法加载指定的元数据资源
【发布时间】:2012-01-23 12:35:06
【问题描述】:

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

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

【问题讨论】:

标签: c# entity-framework


【解决方案1】:

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

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

收件人:

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

它有效:-)

【讨论】:

  • 看起来不错的解决方案,但这样做有什么坏处吗?
  • 我不知道。从那时到今天,我一直在使用它;)
  • 只要你只有一个模型,这应该不是问题。这为我解决了! +1
  • 抛出 ArgumentException:参数 'xmlReader' 无效。必须提供至少一个 .ssdl 工件。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2014-06-13
    • 1970-01-01
    • 2015-05-15
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多