简体   繁体   中英

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

I'm quite new to .net and entity framework (this is my first project) and I'm getting the following error when trying to update the database:

    *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.*

I tried doing what it says (at least I think so) by adding the following to my dbContext class:

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

Not sure have I formulated that method correctly but it did not help (I tried with different DeleteBehavior like SetNull and NoAction)

The thing that really got me confused is that the issue appears even after removing all fields related to other tables from Rating class or even all references between all classes.

My Rating class:

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
}

User class:

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

Game class:

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 class (probably unrelated to the issue just wanted to give a full context)

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 class:

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

}

The issue only appeared after I tried to update the database. The first few migrations and updates were ok, however, then I tried adding [Required] annotation (you can see them commented in the above code) as I noticed that most of the fields were created as nullable in my database - after that the issue starting to occur even after removing the annotations.

In case that matters, I'm using Visual Studio 2019 and SQL Server Express

Does anyone have any idea what may be the cause of this?

Edit: Image of of my database schema diagram from SSMS

As you can see in the database schema it's visible that there are indeed cycles in the database, however, I cannot get rid of them as Entity Framework's command "Update-Database" does not update the DB and just throws the error mentioned above.

Based on my test, you can try the following steps to solve the problem.

First, please change your dbcontext class into the following code.

 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)
    {

    }

}

Second, please delete all the tables the database.

Third, please try the following command in your package console.

PM> Update-Database -Force

Finally, you can see the new tables in the databse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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