多個級聯路徑錯誤 - 實體框架核心

asp.net-core c# entity-framework-core

我正在嘗試使用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."

我不打算實際刪除數據庫中的任何內容,但如果用戶被刪除,我可以設置刪除與用戶相關的所有內容。

這是我的模特:

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; }
}

我在DbContext中嘗試了沒有運氣的跟隨:

1)

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

2)

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

3)

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

我需要做什麼才能正確設置此設置?

一般承認的答案

我認為這可以在你的Answer實體中解決,將DisputeId可為空:

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

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

並以這種方式配置您的關係:

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) ;
}

第二種關係按慣例是可選的,因此應解決您的問題。

來自EF核心文檔:

按照慣例,級聯刪除將設置為Cascade以獲取所需關係,並限制為可選關係。級聯意味著也會刪除依賴實體。限制意味著未加載到內存中的從屬實體將保持不變,必須手動刪除或更新為指向有效的主體實體。對於加載到內存中的實體,EF將嘗試將外鍵屬性設置為null。

如果您的實體類中有外鍵屬性,則根據外鍵屬性是必需還是可選來確定關係的必要性



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow