C# 对从ApplicationUser(IdentityUser)继承的类进行级联删除

C# 对从ApplicationUser(IdentityUser)继承的类进行级联删除,c#,asp.net,.net,asp.net-mvc,entity-framework,C#,Asp.net,.net,Asp.net Mvc,Entity Framework,我有两个类是从ASP.NET Identity中的ApplicationUser继承的, 课程如下: 这是我的ApplicationUser类 public class ApplicationUser : IdentityUser { public string Name { get; set; } public string Surname { get; set; } public async Task<ClaimsIdentity> GenerateU

我有两个类是从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;
    }
}
公共类应用程序用户:IdentityUser
{
公共字符串名称{get;set;}
公共字符串姓氏{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
以及继承它的两个类:

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; }
}
公共类用户:应用程序用户
{
公共虚拟ICollection修复{get;set;}
公共虚拟ICollection车辆{get;set;}
}
公共类修复程序:ApplicationUser
{
公共虚拟ICollection修复{get;set;}
}
当我运行代码第一次迁移时,这两个类都在同一个表中,这是一个具有适当鉴别器和角色的AspNetUsers表。问题是,当我从系统中删除一些用户时,我也希望从数据库中删除所有车辆和维修,但由于在普通数据库中,除了是哪个用户的鉴别器之外,没有适当的区别,所以我没有找到一种方法来设置引用数据库中适当表的外键约束的级联删除(车辆和维修)因此,要么我得到了一个异常,要么我在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添加了以下流体API代码:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<HumanUser>().HasOptional(x => x.GlobalInventory).WithRequired(x => x.User).WillCascadeOnDelete(true);
        base.OnModelCreating(modelBuilder);
    }
但是-如果我将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);
    }
(当ApplicationUser被定义为抽象类时,这也可以正常工作)


不过,我不想将特定的实体属性附加到基类,所以这就是我目前所处的困境。这就好像当应用于我的继承类时,删除指定上的流体API级联被忽略了一样,但当应用于基类时就不被忽略了。

试图解决一个类似的问题,并在这里结束,所以我将贡献什么到目前为止我已经了解了。在我的例子中,我有一个基本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添加了以下流体API代码:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<HumanUser>().HasOptional(x => x.GlobalInventory).WithRequired(x => x.User).WillCascadeOnDelete(true);
        base.OnModelCreating(modelBuilder);
    }
但是-如果我将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);
    }
(当ApplicationUser被定义为抽象类时,这也可以正常工作)

不过,我不想将特定的实体属性附加到基类,所以这就是我目前所处的位置。这就好像当应用于我的继承类时,删除指定时的流体API级联被忽略了,但当应用于基类时就不被忽略了