簡體   English   中英

即使在完全刪除受影響的字段后,在表上引入 FOREIGN KEY 約束也可能導致循環或多個級聯路徑

[英]Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths even after removing the affected field completely

我對 .net 和實體框架很陌生(這是我的第一個項目),嘗試更新數據庫時出現以下錯誤:

    *Introducing FOREIGN KEY constraint 'FK_Rating_User_UserId' on table 'Rating' 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.*

我嘗試通過將以下內容添加到我的 dbContext 類來做它所說的(至少我是這么認為的):

   protected override void OnModelCreating(ModelBuilder modelbuilder)
        {
            modelbuilder.Entity<Rating>().HasOne(u => u.User).WithMany().OnDelete(DeleteBehavior.Restrict);
            modelbuilder.Entity<Rating>().HasOne(g => g.Game).WithMany().OnDelete(DeleteBehavior.Restrict);
        }

不確定我是否正確地制定了該方法,但它沒有幫助(我嘗試了不同的 DeleteBehavior,如 SetNull 和 NoAction)

真正讓我感到困惑的是,即使從 Rating 類中刪除與其他表相關的所有字段,甚至所有類之間的所有引用,也會出現問題。

我的評分等級:

public class Rating
{
    public long RatingId { get; set; }
    //[Rating]
    public virtual Game Game { get; set; } // issue appears even after removing this and User line
    //[Rating]
    public int Score { get; set; }
    public string CommentTitle { get; set; }
    public string CommentDescription { get; set; }
    //[Rating]
    public virtual User User { get; set; }// issue appears even after removing this and Game line
}

用戶類:

public class User
{
    public long UserId { get; set; }
    //[Required]
    public bool IsModerator { get; set; }
    //[Required]
    public string Username { get; set; }
    //[Required]
    public string Email { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    //[Required]
    public string Password { get; set; }
    //[Required]
    public string Salt { get; set; }
    public string Description { get; set; }
}

游戲類:

public class Game
{
    public long GameId { get; set; }
    //[Required]
    public virtual User User { get; set; }
    //[Required]
    public string Title { get; set; }
    public string Description { get; set; }
    //[Required]
    public string PricingType { get; set; }
    public float MinDonation { get; set; }
    public float MaxDonation { get; set; }
    //[Required]
    public string FileLocation { get; set; }
    public float AverageRaiting { get; set; }
    public int DownloadCount { get; set; }
}

GameImage 類(可能與問題無關,只是想提供完整的上下文)

public class GameImage
{
    public long GameImageId { get; set; }
    //[Required]
    public virtual Game Game { get; set; }
    //[Required]
    public string Location { get; set; }
    //[Required]
    public bool IsThumbnail { get; set; }
}

dbContext 類:

    public class dbContext : DbContext
{
    public dbContext(DbContextOptions<dbContext> options) : base(options)
    {
    }

    public DbSet<User> User { get; set; }
    public DbSet<Rating> Rating { get; set; }
    public DbSet<GameImage> GameImage { get; set; }
    public DbSet<Game> Game { get; set; }

}

該問題僅在我嘗試更新數據庫后出現。 前幾次遷移和更新沒問題,但是,然后我嘗試添加 [Required] 注釋(您可以在上面的代碼中看到它們的注釋),因為我注意到大多數字段在我的數據庫中創建為可為空 - 之后問題即使在刪除注釋后也開始發生。

如果重要,我使用的是 Visual Studio 2019 和 SQL Server Express

有誰知道這可能是什么原因?

編輯:來自 SSMS 的我的數據庫架構圖的圖像

正如您在數據庫架構中所看到的,可以看到數據庫中確實存在循環,但是,我無法擺脫它們,因為實體框架的命令“Update-Database”不會更新數據庫,只會引發上述錯誤。

根據我的測試,您可以嘗試以下步驟來解決問題。

首先,請將您的 dbcontext 類更改為以下代碼。

 public class dbContext : DbContext
{
    public dbContext() : base("name=MyContext") { }
    public DbSet<User> User { get; set; }
    public DbSet<Rating> Rating { get; set; }
    public DbSet<GameImage> GameImage { get; set; }
    public DbSet<Game> Game { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

    }

}

二、請刪除數據庫中的所有表。

第三,請在您的軟件包控制台中嘗試以下命令。

PM> Update-Database -Force

最后,您可以在數據庫中看到新表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM