条目(数据库)已经添加

时间:2018-07-25 10:06:05

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

我有两个共享同一数据库的应用程序,分别是APIMVC5应用程序。两者都可以在localhost上很好地运行,但是在部署到我的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 });

    }

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

更新

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

enter image description here

我也将Initial Catalog添加到了连接字符串中 XLAB

1 个答案:

答案 0 :(得分:2)

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