Asp.net 在表上引入外键约束可能会导致循环或多个级联路径,即使在完全删除受影响的字段之后也是如此

Asp.net 在表上引入外键约束可能会导致循环或多个级联路径,即使在完全删除受影响的字段之后也是如此,asp.net,visual-studio,entity-framework,constraints,Asp.net,Visual Studio,Entity Framework,Constraints,我对.net和entity framework非常陌生(这是我的第一个项目),在尝试更新数据库时出现以下错误: *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

我对.net和entity framework非常陌生(这是我的第一个项目),在尝试更新数据库时出现以下错误:

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

}
用户类别:

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类(可能与问题无关,只是想给出完整的上下文)

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

}
公共类dbContext:dbContext
{
公共dbContext(DbContextOptions):基本(选项)
{
}
公共数据库集用户{get;set;}
公共数据库集评级{get;set;}
公共DbSet GameImage{get;set;}
公共DbSet游戏{get;set;}
}
这个问题是在我试图更新数据库后才出现的。最初的几次迁移和更新都是正常的,但是,我尝试添加[Required]注释(您可以在上面的代码中看到注释),因为我注意到大多数字段在我的数据库中创建为可为空的-之后,即使删除注释,问题也开始出现

在这种情况下,我使用的是Visual Studio 2019和SQL Server Express

有人知道这可能是什么原因吗

编辑:


正如您在数据库模式中看到的,数据库中确实存在循环,但是,我无法消除它们,因为Entity Framework的命令“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)
    {

    }

}

最后,您可以在数据库中看到新表。

根据我的测试,我无法重现您的问题。您能告诉我您在代码中把[Required]注释放在哪里吗?您可以在问题中编辑它。错误消息看起来像来自数据库的消息。这些表的数据库架构是什么。请将其添加到您的帖子中。@JackJJun MSFT-刚刚更新了代码块,将[Required]属性作为注释包含进去。@sailkrafiq-添加了一个包含所有表的架构图的图像。奇怪的是,确实存在循环,但是,我无法摆脱它们,因为“updatedatabase”命令不更新循环,而只是抛出上面提到的错误。如果我自己通过SSMS删除连接/更新数据库,可以吗?然后更新我的实体以匹配它?或者这是一个可怕的想法?好吧,现在就看。你需要做的就是这篇文章的建议。基本上,您必须删除级联并替换为触发器
PM> Update-Database -Force