【问题标题】:The entry (Database) has already been added条目(数据库)已添加
【发布时间】:2018-07-25 10:06:04
【问题描述】:

我有两个应用程序共享同一个数据库,即 APIMVC5 应用程序。两者都在本地主机上运行良好,但在部署到我的 Azure 帐户时出现此错误

 Configuration Error

Description: An error occurred during the processing of a configuration
             file required to service this request. Please review the
             specific error details below and modify your configuration file appropriately.

Parser Error Message: The entry 'XlabDatabase' has already been added.

Source Error:

 An application error occurred on the server. The current custom error settings
 for this application prevent the details of the application error from being viewed remotely (for security reasons)

我已采取措施甚至重命名数据库,但在部署时我使用相同的远程字符串

这是我的 MVC 应用程序连接字符串

<connectionStrings>
   <add name="XlabDatabase_acc" providerName="System.Data.SQLClient" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;MultipleActiveResultSets=true;App=EntityFramework" />
</connectionStrings>

这是我的 MVC 上下文

    public virtual DbSet<Account> Accounts { get; set; }
    public virtual DbSet<AnnualReport> AnnualReports { get; set; }
    public virtual DbSet<MonthlyReport> MonthlyReports { get; set; }
    public virtual DbSet<WeekReport> WeekReports { get; set; }

    protected override void OnModelCreating(DbModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.Entity<AnnualReport>().HasKey<int>(x => x.AnnualReportID);
        builder.Entity<MonthlyReport>().HasKey<int>(x => x.MonthlyReportID);
        builder.Entity<WeekReport>().HasKey<int>(x => x.ReportID);

    }

这是我的 API 连接字符串

  <connectionStrings>
     <add name="XlabDatabase" providerName="System.Data.SQLClient" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;MultipleActiveResultSets=true;" />
  </connectionStrings>

这是我的 API 上下文

 public virtual DbSet<Customers> Customers { get; set; }
    public virtual DbSet<Salesman> Salesman { get; set; }
    public virtual DbSet<Route> Route { get; set; }
    public virtual DbSet<Purchase> Purchase { get; set; }
    public virtual DbSet<Products> Products { get; set; }
    public virtual DbSet<Assets> Assets { get; set; }
    public virtual DbSet<WeekReports> WKReports { get; set; }
    public virtual DbSet<MonthlyReports> MONReports { get; set; }
    public virtual DbSet<AnnualReports> ANNReports { get; set; }
    public virtual DbSet<Accounts> Accounts { get; set; }
    public virtual DbSet<LoadingOrder> LoadingOrder { get; set; }


    protected override void OnModelCreating(DbModelBuilder builder)
    {

        base.OnModelCreating(builder);

        builder.Conventions.Remove<PluralizingTableNameConvention>();
        builder.Entity<LoadingOrder>().HasRequired(c => c.Salesman).WithMany().WillCascadeOnDelete(false);
        builder.Entity<IdentityUserClaim>().ToTable("UserClaim").HasKey<Int32>(r => r.Id);
        builder.Entity<IdentityUserLogin>().ToTable("UserLogin").HasKey<string>(l => l.UserId);
        builder.Entity<IdentityRole>().ToTable("Role").HasKey<string>(r => r.Id);
        builder.Entity<User>().ToTable("User").HasKey(r => new{ r.IDNumber, r.UserName});
        builder.Entity<IdentityUser>().ToTable("User").HasKey<string>(r => r.UserName);
        builder.Entity<IdentityUserRole>().ToTable("UserRole").HasKey(r => new { r.RoleId, r.UserId });

    }

我已将这两个应用程序部署到两个不同的虚拟应用程序和目录中。

更新

这就是我部署它们共享相同连接字符串的两个应用程序的方式

我还在连接字符串中添加了Initial Catalog XLAB

【问题讨论】:

  • 您将 MVC 应用程序和 API 部署到 Azure 并使用此连接字符串连接到数据库。首先这需要改变,你需要连接到一个 azure sql/on-premise sql db(如果你有必要的基础设施设置)。当您将应用程序移动到 azure(应用程序服务或 VM)时,当前连接字符串将不起作用 其次我在连接字符串中没有看到“初始目录”(数据库名称),它在您的原始代码中吗?跨度>
  • 我已经添加了初始目录,但没有任何改变。至于连接字符串,我已经用我如何使用相同的连接字符串部署两个应用程序的图像更新了我的问题

标签: c# entity-framework azure deployment azure-sql-database


【解决方案1】:

在连接字符串上,在&lt;providers&gt; 之后和第一个&lt;add....&gt; 节点之前,添加一个&lt;clear /&gt; 节点。

【讨论】:

  • 祝你有美好的一天!
  • 这是什么意思?我在任何地方都看不到“提供者”。
  • 在 API 的连接字符串上。
猜你喜欢
  • 2014-05-11
  • 1970-01-01
  • 2019-12-29
  • 2012-07-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 1970-01-01
相关资源
最近更新 更多