public class ApplicationDbContext : IdentityDbContext
{
public DbSet<RightsManagementLog> rightsManagementLogs { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
public class RightsManagementLog
{
[Key] public long Id { get; set; }
// The user who changed other user's rights
public string adminId { get; set; }
[Required, ForeignKey("adminId")] public IdentityUser admin { get; set; }
// The user whose rights were changed
public string userId { get; set; }
[Required, ForeignKey("userId")] public IdentityUser user { get; set; }
}
此类代码会产生以下结果:
Introducing FOREIGN KEY constraint 'FK_rightsManagementLogs_AspNetUsers_userId' on table 'rightsManagementLogs' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
已经有一个问题可以解决类似的问题:Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?,答案是按照以下方式做点事情:(引用公认的答案):
modelBuilder.Entity<Card>()
.HasRequired(c => c.Stage)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Side>()
.HasRequired(s => s.Stage)
.WithMany()
.WillCascadeOnDelete(false);
但是,尽管这可能在Entity Framework中起作用(因为该问题被标记为entity-framework
),但我在Entity Framework Core中找不到类似HasRequired
或WillCascadeOnDelete
的方法。
因此,我认为应该问一个单独的问题:如何在EF Core中解决此问题?