2016-12-24 206 views
1

我有從ApplicationUser繼承了ASP .NET身份兩班從ApplicationUser(IdentityUser)繼承類, 類如下所示:級聯刪除

這是我ApplicationUser類

public class ApplicationUser : IdentityUser 
{ 
    public string Name { get; set; } 

    public string Surname { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

和兩個類,繼承了它:

public class User : ApplicationUser 
{ 
    public virtual ICollection<Repair> Repairs { get; set; } 

    public virtual ICollection<Vehicle> Vehicles { get; set; } 
} 

public class Repairer : ApplicationUser 
{ 
    public virtual ICollection<Repair> Repairs { get; set; } 
} 

當我運行代碼首先遷移兩個類是相同的表裏面whic h是具有適當鑑別器和角色的AspNetUsers表。問題是,當我從系統中刪除一些用戶時,我也希望從我的數據庫中刪除所有車輛和維修,但由於在普通數據庫中沒有適當的區別,除了哪個用戶是鑑別者之外,我沒有找到設置方法級聯刪除引用數據庫(車輛和修復)中的適當表的外鍵約束,所以而不是我得到一個異常,或者我在db中的外鍵被設置爲空。

這是一種實現級聯刪除的方法,或者我應該更改我的模型,因爲除此之外,所有工作都正常。

在此先感謝!

回答

0

試圖解決類似的問題,並在這裏結束,所以我會貢獻我迄今爲止學到的東西。在我來說,我有含有特定的(實體)性質的兩個繼承類的基ApplicationUser:

public class ApplicationUser : IdentityUser { 

} 

public class Server : ApplicationUser { 

} 

public class HumanUser : ApplicationUser { 

    [Required] 
    public virtual GlobalInventory GlobalInventory { get; set; } 
} 

public class GlobalInventory { 

    [Key, ForeignKey("User")] 
    public string UserId { get; set; } 

    [Required] 
    public virtual HumanUser User { get; set; } 
} 

我還添加了以下流體API代碼ApplicationDbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<HumanUser>().HasOptional(x => x.GlobalInventory).WithRequired(x => x.User).WillCascadeOnDelete(true); 
     base.OnModelCreating(modelBuilder); 
    } 

這裏的移民部分Code First將爲GlobalInventory進行調整。注意與HumanUser的外鍵連接沒有「cascadeDelete:true」標誌。這意味着,當我刪除父,會出現錯誤:

 CreateTable(
      "dbo.GlobalInventories", 
      c => new 
       { 
        UserId = c.String(nullable: false, maxLength: 128), 
       }) 
      .PrimaryKey(t => t.UserId) 
      .ForeignKey("dbo.Users", t => t.UserId) 
      .Index(t => t.UserId); 

但是 - 如果我做基ApplicationUser類的GlobalInventory一個屬性,而不是像這樣:

public class ApplicationUser : IdentityUser { 

    [Required] 
    public virtual GlobalInventory GlobalInventory { get; set; } 
} 

public class HumanUser : ApplicationUser { 

    [Required] 
    public virtual GlobalInventory GlobalInventory { get; set; } 
} 


public class GlobalInventory { 

    [Key, ForeignKey("User")] 
    public string UserId { get; set; } 

    [Required] 
    public virtual ApplicationUser User { get; set; } 
} 

..和修改因此建立模型:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<ApplicationUser>().HasOptional(x => x.GlobalInventory).WithRequired(x => x.User).WillCascadeOnDelete(true); 
     base.OnModelCreating(modelBuilder); 
    } 

那麼對於GlobalInventory遷移代碼看起來正確的,我可以刪除明知GlobalInventory也將被刪除的用戶:

 CreateTable(
      "dbo.GlobalInventories", 
      c => new 
       { 
        UserId = c.String(nullable: false, maxLength: 128), 
       }) 
      .PrimaryKey(t => t.UserId) 
      .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true) 
      .Index(t => t.UserId); 

(當ApplicationUser是作爲抽象類定義這也能正常工作)

我不想雖然具體的實體的屬性附加到基類,所以這是我目前卡在。就好像Fluid API級聯刪除指定在應用於繼承類時被忽略,但在應用於基類時不會被忽略。