代码之家  ›  专栏  ›  技术社区  ›  John Nyingi

条目(数据库)已添加

  •  1
  • John Nyingi  · 技术社区  · 6 年前

    我有两个应用程序共享同一个数据库 API 还有一个 MVC5 应用程序。两者在本地主机上运行良好,但在部署到我的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 回复  |  直到 6 年前
        1
  •  2
  •   Alberto Morillo    6 年前

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