繁体   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