![](/img/trans.png)
[英]There is no implicit reference conversion from ApplicationUser to 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.