EF Core,配置modelBuilder以修复错误“循环或多个级联路径”。 [英] EF Core, configure modelBuilder to fix error "cycles or multiple cascade paths"

查看:286
本文介绍了EF Core,配置modelBuilder以修复错误“循环或多个级联路径”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要配置以下关系:


  • 像可能有一个原因(类型注释),如果需要,此注释必须被级联删除Like将被删除

  • Like可能有一个Comment(类型为Comment)或User(类型User),Like必须级联删除,如果Comment或User将被删除

  • 赞不能同时具有Comment(评论类型)和User(用户类型)

  • Author(用户类型)不能创建Likes重复项

  • Like may has a Reason (type Comment) and this Comment have to be cascade deleated if Like will be deleated
  • Like may have a Comment (type Comment) or User (type User), Like have to be cascade deleated if Comment or User will be deleated
  • Like must not have both Comment (type Comment) and User (type User)
  • Author (type User) must not be able to create Likes duplicates

我尝试了很多组合但都没有成功,请帮助我。

I tried many combinations but did not have a success, please help me.

我当前代码:

public class Comment
{
    public int Id { get; set; }
    public string Text { get; set; }
    public Like ToLike { get; set; }
    public int? ToLikeId { get; set; }
    public ICollection<Like> Likes { get; set; }
}

public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public ICollection<Like> CreatedLikes { get; set; }
    public ICollection<Like> Likes { get; set; }
}

public class Like
{
    public int Id { get; set; }

    public User Author { get; set; }
    public int AuthorId { get; set; }

    public DateTime CreatedAt { get; set; }

    public Comment Reason { get; set; }

    public Comment Comment { get; set; }
    public int? CommentId { get; set; }

    public User User { get; set; }
    public int? UserId { get; set; }
}

        modelBuilder.Entity<Like>(b =>
        {
            b.HasAlternateKey(l => new { l.AuthorId, l.CommentId, l.UserId });
            b.HasOne(l => l.Reason)
            .WithOne(c => c.ToLike)
            .HasForeignKey<Comment>(c => c.ToLikeId)
            .OnDelete(DeleteBehavior.SetNull);

            b.HasOne(l => l.Author).WithMany(a => a.CreatedLikes)
                .OnDelete(DeleteBehavior.Restrict);

            b.HasOne(l => l.User).WithMany(u => u.Likes);
            b.HasOne(l => l.Comment)
            .WithMany(c => c.Likes)
            .HasForeignKey(c => c.CommentId);
        });

当前在第一个数据库初始化(行context.Database.Migrate();)中出现此错误:

Currently I have this error on first DB initialize (row context.Database.Migrate();):


System.Data.SqlClient.SqlException:'在表'Likes'上引入FOREIGN KEY
约束'FK_Likes_Comments_CommentId'可能导致
个循环或多个级联路径。指定ON DELETE NO ACTION或ON
UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

System.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_Likes_Comments_CommentId' on table 'Likes' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

主要问题:如何要解决此错误?

The main question: how to fix this error?

其他问题:


  • 如何配置我的全部

  • 使Likes具有用户活动历史记录的最佳数据库模式是什么?

推荐答案

如果我正确理解此问题,则在创建具有级联更新和删除的主键和外键关系时,您会遇到菱形图案。在具有级联关系的模式中,数据库发现级联模棱两可。

If I understand the issue correctly, you are experiencing the diamond pattern when you are creating primary key and foreign key relationships that have cascading updates and deletes. In the pattern with cascading relationships the database finds the cascading ambiguous.

菱形中的一个关系必须具有外键的Restrict属性。

One of the relationships in the diamond must have the Restrict attribute for the foreign key.

有关实体框架核心关系的Microsoft文档


级联删除

Cascade Delete

按照惯例,对于必需的
关系,级联删除将设置为Cascade,对于可选关系,将级联删除设置为Restrict(请参阅必需的
部分以了解必需和可选
关系之间的区别)。级联表示依赖实体也将被删除。
限制意味着未加载到内存
中的从属实体将保持不变,必须手动删除或更新为指向有效主体实体的
。对于加载到
内存中的实体,EF会尝试将外键属性设置为null。

By convention, cascade delete will be set to Cascade for required relationships and Restrict for optional relationships (see the Required section for the difference between required and optional relationships). Cascade means dependent entities are also deleted. Restrict means that dependent entities that are not loaded into memory will remain unchanged and must be manually deleted, or updated to point to a valid principal entity. For entities that are loaded into memory, EF will attempt to set the foreign key properties to null.

这篇关于EF Core,配置modelBuilder以修复错误“循环或多个级联路径”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆