EF Core执行Update-Database时遇外键循环或多级联路径错误求助
问题:EF Core执行Update-Database时外键约束引发循环/多联级路径错误
Introducing FOREIGN KEY constraint 'FK_Comments_AspNetUsers_AuthorId' on table 'Comments' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
本人是Entity Framework Core及数据库领域的新手,已为此问题困扰数小时。
模型代码
public class ApplicationUser : IdentityUser { public List<Post> Posts { get; set; } = new(); public List<Comment> Comments { get; set; } = new(); public List<Post> UpvotedPosts { get; set; } = new(); public List<Comment> UpvotedComments { get; set; } = new(); public List<Post> SavedPosts { get; set; } = new(); } public class Post { public int ID { get; set; } public string Title { get; set; } public string URL { get; set; } public DateTime DateCreated { get; set; } = DateTime.Now; public int Upvotes { get; set; } public ApplicationUser Author { get; set; } public List<Comment> Comments { get; set; } = new(); public List<ApplicationUser> UsersUpvoting { get; set; } = new(); public List<ApplicationUser> UsersSaving { get; set; } = new(); } public class Comment { public int ID { get; set; } public string Content { get; set; } public int Upvotes { get; set; } public DateTime DateCreated { get; set; } = DateTime.Now; public ApplicationUser Author { get; set; } public Post Post { get; set; } public List<ApplicationUser> UsersUpvoting { get; set; } = new(); }
DbContext代码
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<Post>() .HasOne(p => p.Author) .WithMany(u => u.Posts); builder.Entity<Post>() .HasMany(p => p.Comments) .WithOne(c => c.Post); builder.Entity<Post>() .HasMany(p => p.UsersUpvoting) .WithMany(u => u.UpvotedPosts); builder.Entity<Post>() .HasMany(p => p.UsersSaving) .WithMany(u => u.SavedPosts); builder.Entity<Comment>() .HasOne(c => c.Author) .WithMany(u => u.Comments); builder.Entity<Comment>() .HasOne(c => c.Post) .WithMany(p => p.Comments); builder.Entity<Comment>() .HasMany(c => c.UsersUpvoting) .WithMany(u => u.UpvotedComments); builder.Entity<ApplicationUser>() .HasMany(u => u.Posts) .WithOne(p => p.Author); builder.Entity<ApplicationUser>() .HasMany(u => u.Comments) .WithOne(c => c.Author); builder.Entity<ApplicationUser>() .HasMany(u => u.UpvotedPosts) .WithMany(p => p.UsersUpvoting); builder.Entity<ApplicationUser>() .HasMany(u => u.UpvotedComments) .WithMany(c => c.UsersUpvoting); builder.Entity<ApplicationUser>() .HasMany(u => u.SavedPosts) .WithMany(p => p.UsersSaving); } public DbSet<Post> Posts { get; set; } public DbSet<Comment> Comments { get; set; } }
已尝试的操作
我尝试在Comment与Author的关联配置中添加.OnDelete(DeleteBehavior.NoAction),代码如下:
builder.Entity<Comment>() .HasOne(c => c.Author) .WithMany(u => u.Comments) .OnDelete(DeleteBehavior.NoAction);
但仍然出现相同的错误,恳请各位提供帮助,谢谢!
内容的提问来源于stack exchange,提问作者chmorgan
相关产品推荐
相关产品推荐

