2015-06-25 48 views
0

我有一個應用程序,我現在已經意識到,愚蠢地有兩個單獨的DBcontexts,他們需要合併,以允許應用程序實體和ApplicationUser實體之間的關係。ApplicationUser DbContext與我的DBcontext合併

隨着應用程序的開發,我相信對其他類中applicationUser的引用已經在我的DBcontext上爲ApplicationUser實體創建了表,而在另一個連接字符串和上下文中存在AspUserIdentity等表。

我試圖刪除原來的上下文和合並兩個上下文,但試圖創建它要重命名我目前的情況下,以AspUserIdentity ECT所有現有ApplicationUser表遷移時(見下文)

如何我成功地合併了上下文,還是有另一種解決方案?

我不太確定要發佈什麼,但我會嘗試發佈我認爲是相關的,然後如果任何人需要進一步的信息隨時請求它。

這是嘗試合併後的所有代碼。 (使用指南中的這個問題的答案之一:Merge MyDbContext to Asp.net IdentityDbContext

我的背景有如下表: ApplicationUsers IdentityRoles IdentityUserClaims IdentityUserLogins IdentityUserRoles

原始上下文有以下表和遷移正試圖重命名上述表以匹配這些: AspNetRoles AspNetUserClaims AspNetUserLogins AspNetUserRoles AspNetUsers

我的上下文:(該評論的模型構建器發了導致多重錯誤)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

using System.Data.Entity; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Configuration; 
using System.Web.Mvc; 
using Microsoft.AspNet.Identity.EntityFramework; 
using System.Data.Entity.ModelConfiguration.Conventions; 
using eCommSite.Models; 
using System.Data.Entity.Infrastructure; 

namespace eCommSite.Areas.Admin.Models 
{ 
    public class TheDBContext : IdentityDbContext 
    { 

     public TheDBContext() 
       : base(ConfigurationManager.ConnectionStrings["DataDBString"].ConnectionString) 
      { 
       var objectContext = (this as IObjectContextAdapter).ObjectContext; 

       // Sets the command timeout for all the commands 
       objectContext.CommandTimeout = 360; 
      } 
     public DbSet<ProductBase> ProductBases { get; set; } 
     public DbSet<Address> Addresses { get; set; } 
     public DbSet<PromotionImage> PromotionImages { get; set; } 
     public DbSet<Image> Images { get; set; } 
     public DbSet<ImageCollection> ImageCollections { get; set; } 
     public DbSet<Order> Orders { get; set; } 
     public DbSet<OrderLine> OrderLines { get; set; } 
     public DbSet<ProductOption> ProductOptions { get; set; } 
     public DbSet<ProductOptionType> ProductOptionTypes { get; set; } 
     public DbSet<ProductType> ProductTypes { get; set; } 
     public DbSet<FilterValueEntry> FilterValueEntries { get; set; } 
     public DbSet<FilterType> FilterTypesDB { get; set; } 
     public DbSet<CardBaseSummary> CardBaseSummarys { get; set; } 
     public DbSet<ProductBaseSummary> ProductBaseSummaries { get; set; } 
     public DbSet<Category> Categories { get; set; } 
     public DbSet<Brand> Brands { get; set; } 
     public DbSet<MMHPriceEntry> MMHPriceEntries { get; set; } 
     public DbSet<Legality> Legalities { get; set; } 
     public DbSet<ForeignName> ForeignNames { get; set; } 
     public DbSet<SKUStockQuantity> SKUStockQuantities { get; set; } 
     public DbSet<Location> Locations { get; set; } 
     public DbSet<Cart> Carts { get; set; } 
     public DbSet<StockCollection> StockCollections { get; set; } 

     public static TheDBContext Create() 
     { 
      return new TheDBContext(); 
     } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 

      modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>(); 
      modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 


      // modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId); 
      // modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id); 
     // modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }); 

     } 

     public System.Data.Entity.DbSet<eCommSite.Areas.Admin.Models.ApplicationUser> ApplicationUsers { get; set; } 

     public System.Data.Entity.DbSet<eCommSite.Areas.Admin.Models.ProductRelation> ProductRelations { get; set; } 

     public System.Data.Entity.DbSet<eCommSite.Areas.Admin.Models.MTG_Cards> MTG_Cards { get; set; } 

     public System.Data.Entity.DbSet<eCommSite.Areas.Admin.Models.MTG_Set> MTG_Set { get; set; } 

     public System.Data.Entity.DbSet<eCommSite.Areas.Admin.Models.ProductOptionValue> ProductOptionValues { get; set; } 

     public System.Data.Entity.DbSet<eCommSite.Areas.Admin.Models.SKU> SKUs { get; set; } 

     public System.Data.Entity.DbSet<eCommSite.Areas.Admin.Models.MMHsetToJsonSetReference> MMHsetToJsonSetReferences { get; set; } 

    } 
} 

遷移:

namespace eCommSite.Migrations 
{ 
    using System; 
    using System.Data.Entity.Migrations; 

    public partial class MergeMigration : DbMigration 
    { 
     public override void Up() 
     { 
      RenameTable(name: "dbo.ApplicationUsers", newName: "AspNetUsers"); 
      RenameTable(name: "dbo.IdentityUserClaims", newName: "AspNetUserClaims"); 
      RenameTable(name: "dbo.IdentityUserLogins", newName: "AspNetUserLogins"); 
      RenameTable(name: "dbo.IdentityUserRoles", newName: "AspNetUserRoles"); 
      RenameTable(name: "dbo.IdentityRoles", newName: "AspNetRoles"); 
      DropIndex("dbo.AspNetUserClaims", new[] { "ApplicationUser_Id" }); 
      DropIndex("dbo.AspNetUserLogins", new[] { "ApplicationUser_Id" }); 
      DropIndex("dbo.AspNetUserRoles", new[] { "ApplicationUser_Id" }); 
      DropIndex("dbo.AspNetUserRoles", new[] { "IdentityRole_Id" }); 
      DropColumn("dbo.AspNetUserClaims", "UserId"); 
      DropColumn("dbo.AspNetUserLogins", "UserId"); 
      DropColumn("dbo.AspNetUserRoles", "UserId"); 
      DropColumn("dbo.AspNetUserRoles", "RoleId"); 
      RenameColumn(table: "dbo.AspNetUserClaims", name: "ApplicationUser_Id", newName: "UserId"); 
      RenameColumn(table: "dbo.AspNetUserLogins", name: "ApplicationUser_Id", newName: "UserId"); 
      RenameColumn(table: "dbo.AspNetUserRoles", name: "ApplicationUser_Id", newName: "UserId"); 
      RenameColumn(table: "dbo.AspNetUserRoles", name: "IdentityRole_Id", newName: "RoleId"); 
      DropPrimaryKey("dbo.AspNetUserLogins"); 
      DropPrimaryKey("dbo.AspNetUserRoles"); 
      AddColumn("dbo.AspNetUsers", "Discriminator", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetUsers", "Newsletter", c => c.Boolean()); 
      AlterColumn("dbo.AspNetUsers", "RewardPoints", c => c.Int()); 
      AlterColumn("dbo.AspNetUsers", "BasketId", c => c.Int()); 
      AlterColumn("dbo.AspNetUsers", "BuylistBasketId", c => c.Int()); 
      AlterColumn("dbo.AspNetUsers", "StoreCredit", c => c.Double()); 
      AlterColumn("dbo.AspNetUsers", "Email", c => c.String(maxLength: 256)); 
      AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false, maxLength: 256)); 
      AlterColumn("dbo.AspNetUserClaims", "UserId", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetUserClaims", "UserId", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetUserLogins", "LoginProvider", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetUserLogins", "ProviderKey", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetUserLogins", "UserId", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetUserRoles", "UserId", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetUserRoles", "RoleId", c => c.String(nullable: false, maxLength: 128)); 
      AlterColumn("dbo.AspNetRoles", "Name", c => c.String(nullable: false, maxLength: 256)); 
      AddPrimaryKey("dbo.AspNetUserLogins", new[] { "LoginProvider", "ProviderKey", "UserId" }); 
      AddPrimaryKey("dbo.AspNetUserRoles", new[] { "UserId", "RoleId" }); 
      CreateIndex("dbo.AspNetUsers", "UserName", unique: true, name: "UserNameIndex"); 
      CreateIndex("dbo.AspNetUserClaims", "UserId"); 
      CreateIndex("dbo.AspNetUserLogins", "UserId"); 
      CreateIndex("dbo.AspNetUserRoles", "UserId"); 
      CreateIndex("dbo.AspNetUserRoles", "RoleId"); 
      CreateIndex("dbo.AspNetRoles", "Name", unique: true, name: "RoleNameIndex"); 
     } 

     public override void Down() 
     { 
      DropIndex("dbo.AspNetRoles", "RoleNameIndex"); 
      DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" }); 
      DropIndex("dbo.AspNetUserRoles", new[] { "UserId" }); 
      DropIndex("dbo.AspNetUserLogins", new[] { "UserId" }); 
      DropIndex("dbo.AspNetUserClaims", new[] { "UserId" }); 
      DropIndex("dbo.AspNetUsers", "UserNameIndex"); 
      DropPrimaryKey("dbo.AspNetUserRoles"); 
      DropPrimaryKey("dbo.AspNetUserLogins"); 
      AlterColumn("dbo.AspNetRoles", "Name", c => c.String()); 
      AlterColumn("dbo.AspNetUserRoles", "RoleId", c => c.String(maxLength: 128)); 
      AlterColumn("dbo.AspNetUserRoles", "UserId", c => c.String(maxLength: 128)); 
      AlterColumn("dbo.AspNetUserLogins", "UserId", c => c.String(maxLength: 128)); 
      AlterColumn("dbo.AspNetUserLogins", "ProviderKey", c => c.String()); 
      AlterColumn("dbo.AspNetUserLogins", "LoginProvider", c => c.String()); 
      AlterColumn("dbo.AspNetUserClaims", "UserId", c => c.String(maxLength: 128)); 
      AlterColumn("dbo.AspNetUserClaims", "UserId", c => c.String()); 
      AlterColumn("dbo.AspNetUsers", "UserName", c => c.String()); 
      AlterColumn("dbo.AspNetUsers", "Email", c => c.String()); 
      AlterColumn("dbo.AspNetUsers", "StoreCredit", c => c.Double(nullable: false)); 
      AlterColumn("dbo.AspNetUsers", "BuylistBasketId", c => c.Int(nullable: false)); 
      AlterColumn("dbo.AspNetUsers", "BasketId", c => c.Int(nullable: false)); 
      AlterColumn("dbo.AspNetUsers", "RewardPoints", c => c.Int(nullable: false)); 
      AlterColumn("dbo.AspNetUsers", "Newsletter", c => c.Boolean(nullable: false)); 
      DropColumn("dbo.AspNetUsers", "Discriminator"); 
      AddPrimaryKey("dbo.AspNetUserRoles", new[] { "RoleId", "UserId" }); 
      AddPrimaryKey("dbo.AspNetUserLogins", "UserId"); 
      RenameColumn(table: "dbo.AspNetUserRoles", name: "RoleId", newName: "IdentityRole_Id"); 
      RenameColumn(table: "dbo.AspNetUserRoles", name: "UserId", newName: "ApplicationUser_Id"); 
      RenameColumn(table: "dbo.AspNetUserLogins", name: "UserId", newName: "ApplicationUser_Id"); 
      RenameColumn(table: "dbo.AspNetUserClaims", name: "UserId", newName: "ApplicationUser_Id"); 
      AddColumn("dbo.AspNetUserRoles", "RoleId", c => c.String(nullable: false, maxLength: 128)); 
      AddColumn("dbo.AspNetUserRoles", "UserId", c => c.String(nullable: false, maxLength: 128)); 
      AddColumn("dbo.AspNetUserLogins", "UserId", c => c.String(nullable: false, maxLength: 128)); 
      AddColumn("dbo.AspNetUserClaims", "UserId", c => c.String()); 
      CreateIndex("dbo.AspNetUserRoles", "IdentityRole_Id"); 
      CreateIndex("dbo.AspNetUserRoles", "ApplicationUser_Id"); 
      CreateIndex("dbo.AspNetUserLogins", "ApplicationUser_Id"); 
      CreateIndex("dbo.AspNetUserClaims", "ApplicationUser_Id"); 
      RenameTable(name: "dbo.AspNetRoles", newName: "IdentityRoles"); 
      RenameTable(name: "dbo.AspNetUserRoles", newName: "IdentityUserRoles"); 
      RenameTable(name: "dbo.AspNetUserLogins", newName: "IdentityUserLogins"); 
      RenameTable(name: "dbo.AspNetUserClaims", newName: "IdentityUserClaims"); 
      RenameTable(name: "dbo.AspNetUsers", newName: "ApplicationUsers"); 
     } 
    } 
} 

的錯誤,當運行更新的數據庫:

The object 'PK_dbo.AspNetUserLogins' is dependent on column 'UserId'. 
ALTER TABLE DROP COLUMN UserId failed because one or more objects access this column. 

回答

0

我知道你不期待這樣的答案,但我強烈的建議是不合並。讓IdentityDbContext活下去,它也會讓你活着。它是一個(唯一的)騙局。你將需要2個獨立的數據庫是在Azure中的額外成本說我也知道,但還是......

  1. 如果試圖生活在IdentityDbContext合併的背景下,將花費很多時間去發現其行爲,其EF使用情況。爲自己備份這個時間。
  2. 更多信息:您的API到標識(用戶,角色,登錄,鎖定等)是顯式類和它們的方法,而不是EF。嚴格地說,你甚至不應該知道Identity子系統的持久性(EF + RDBMS)。 (認爲​​它是一個黑匣子)
  3. 你是對的關係,但又一次:如果你有一個黑匣子身份子系統,它提供所有服務(包括用戶/密碼/配置文件持久性),你會怎麼做爲使用完全相同的用戶的業務案例構建關係數據庫?只需使用由數據庫中其他子系統提供的用戶標識即可。不要擔心兩個系統之間的參照完整性。考慮以下幾點:在認證會話,你的用戶ID將永遠有效,否則必須在認證過程中嚴重的問題,所以會有擔心最重要的事情...
+0

另一個問題,我有是我想種兩個數據庫,我不知道這兩個上下文有多好。 – HarborneD

+0

另外,例如,地址類具有屬性 public ApplicationUser Owner {get;組; } 如何正確填充它們,如果它們不屬於相同的上下文 – HarborneD

+0

單獨填充。這兩個環境無需互相做任何事情,他們會工作。 –