簡體   English   中英

從ApplicationUser(IdentityUser)繼承的類上的級聯刪除

[英]Cascade delete on classes inherited from ApplicationUser(IdentityUser)

我在ASP .NET Identity中有兩個繼承自ApplicationUser的類,類如下:

這是我的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; }
}

當我運行Code First遷移時,兩個類都在同一個表中,這個表是具有適當的Discriminator和角色的AspNetUsers表。 問題是,當我從系統中刪除一些用戶時,我也希望從我的數據庫中刪除所有車輛和修理但是因為在普通數據庫中沒有正確區分除了哪個用戶的鑒別器之外我沒有找到設置的方法cascade刪除引用數據庫(Vehicle和Repair)中正確表的外鍵約束,所以要么我得到一個異常,要么我的db中的外鍵設置為null。

它是實現級聯刪除的一種方式,還是我應該更改我的模型,因為除此之外一切正常。

提前致謝!

試圖解決類似的問題,並在這里結束,所以我將貢獻我迄今為止學到的東西。 在我的例子中,我有一個基礎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; }
}

我還在ApplicationDbContext中添加了以下Fluid API代碼:

    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);

但是 - 如果我使GlobalInventory成為基本ApplicationUser類的屬性,如下所示:

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 cascade-on-delete指定在應用於我的繼承類時被忽略一樣,但是當應用於基類時卻沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM