级联删除

级联删除

问题描述:

我有从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中的外键被设置为空。

这是一种实现级联删除的方法,或者我应该更改我的模型,因为除此之外,所有工作都正常。

在此先感谢!

试图解决类似的问题,并在这里结束,所以我会贡献我迄今为止学到的东西。在我来说,我有含有特定的(实体)性质的两个继承类的基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级联删除指定在应用于继承类时被忽略,但在应用于基类时不会被忽略。