简体   繁体   English

多个级联路径错误-实体框架核心

[英]Multiple cascading paths Error - Entity Framework Core

I am trying to setup a new database using Entity Framework Core but I am getting an error when calling the command "Update-Database" saying: 我正在尝试使用Entity Framework Core设置新数据库,但是在调用命令“ Update-Database”时遇到错误:

"Introducing FOREIGN KEY constraint 'FK_Answers_Users_UserId' on table 'Answers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints."

I do not plan on actually deleting anything from the database but I would be fine with setting it delete everything related to user if a user is deleted. 我不打算实际从数据库中删除任何内容,但是如果将其删除,则可以将其设置为删除与用户相关的所有内容。

Here are my models: 这是我的模型:

public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public string ImageUrl { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }
    public int AnswersFlagged { get; set; }
    public bool Status { get; set; }

    public IEnumerable<Dispute> Disputes { get; set; }
    public IEnumerable<Answer> Answers { get; set; }
}

public class Dispute
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int CategoryId { get; set; }
    public string Text { get; set; }
    public string OptionOneText { get; set; }
    public string OptionTwoText { get; set; }
    public string OptionThreeText { get; set; }
    public string OptionOneImageUrl { get; set; }
    public string OptionTwoImageUrl { get; set; }
    public string OptionThreeImageUrl { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }
    public bool Status { get; set; }

    public User User { get; set; }
    public Category Category { get; set; }
    public IEnumerable<Answer> Answers { get; set; }
}

public class Answer
{
    public int Id { get; set; }
    public int DisputeId { get; set; }
    public int UserId { get; set; }
    public int SelectedOption { get; set; }
    public string Comment { get; set; }
    public bool Flagged { get; set; }
    public DateTime DateCreated { get; set; }

    public Dispute Dispute { get; set; }
    public User User { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime DateCreated { get; set; }
    public bool Status { get; set; }

    public IEnumerable<Dispute> Disputes { get; set; }
}

public class DisputeCategory
{
    public int Id { get; set; }
    public int DisputeId { get; set; }
    public int CategoryId { get; set; }

    public Dispute Dispute { get; set; }
    public Category Category { get; set; }
}

I have tried the follow with no luck in my DbContext: 我尝试了以下操作,但在DbContext中没有运气:

1) 1)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Answer>()
        .HasOne(p => p.User)
        .WithMany(b => b.Answers)
        .OnDelete(DeleteBehavior.Restrict);
}

2) 2)

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
        modelBuilder.Entity<Answer>()
            .HasOne(p => p.User)
            .WithMany(b => b.Answers)
            .IsRequired(false);
 }

3) 3)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
    {
        relationship.DeleteBehavior = DeleteBehavior.Restrict;
    }
}

What do I need to do to get this setup correctly? 我需要怎么做才能正确设置此设置?

I think this can be solve in you Answer entity, change DisputeId as nullable: 我认为这可以在您的Answer实体中解决,将DisputeId更改为可为空:

public class Answer
{
    public int Id { get; set; }
    public int? DisputeId { get; set; }
    //..

    public Dispute Dispute { get; set; }
    //..
}

And configure your relationships this way: 并以这种方式配置您的关系:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Answer>()
        .HasOne(p => p.User)
        .WithMany(b => b.Answers)
        .HasForeignKey(s =>s.UserId) ;

    modelBuilder.Entity<Answer>()
        .HasOne(p => p.Dispute)
        .WithMany(b => b.Answers)
        .HasForeignKey(s =>s.DisputeId) ;
}

The second relationship would be optional by convention , so that should solve your issue. 根据惯例 ,第二个关系是可选的,因此应该可以解决您的问题。

From EF core documentation: EF核心文档中:

By convention, cascade delete will be set to Cascade for required relationships and Restrict for optional relationships. 按照约定,对于必需的关系,级联删除将设置为Cascade,对于可选的关系,将级联删除设置为Restrict。 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会尝试将外键属性设置为null。

If you have a foreign key property in your entity class then the requiredness of the relationship is determined based on whether the foreign key property is required or optional 如果您的实体类中具有外键属性,则根据外键属性是必需的还是可选的来确定关系的必要性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM