I am designing a database i have three models(user, movie, review).The idea is each user can submit a movie each user can leave a review for a movie.The user model has ICollection and ICollecyion, the movie model has foreign key UserId and ICollection, and the review model has foreign keys for UserId and MovieId. When i try update-database i get this error:
Introducing FOREIGN KEY constraint 'FK_Review_User_UserId' on table 'Review' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Here are my models :
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int FavouriteGenre { get; set; }
public ICollection<Movie> SubmittedMovies { get; set; }
public ICollection<Review> SubmittedReviews { get; set; }
}
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string PosterUrl { get; set; }
public int Year { get; set; }
public int Genre { get; set; }
public DateTime Posted { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public ICollection<Review> Reviews { get; set; }
}
public class Review
{
public int Id { get; set; }
public int Rating { get; set; }
public string UserReview { get; set; }
public int MovieId { get; set; }
public Movie Movie { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public DateTime Posted { get; set; }
}
And my on creating method in the dbContext :
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Review>()
.HasOne(r => r.User)
.WithMany(u => u.SubmittedReviews)
.HasForeignKey(r => r.UserId);
builder.Entity<Review>()
.HasOne(r => r.Movie)
.WithMany(m => m.Reviews)
.HasForeignKey(r => r.MovieId);
}
I have tried this :
builder.Entity<Review>()
.HasOne(r => r.User)
.WithMany(u => u.SubmittedReviews)
.HasForeignKey(r => r.UserId)
.IsRequired(false)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Review>()
.HasOne(r => r.Movie)
.WithMany(m => m.Reviews)
.HasForeignKey(r => r.MovieId)
.IsRequired(false)
.OnDelete(DeleteBehavior.Restrict);
I have also tried making the foreign keys in the Review model nullable(i have tried separately and both at the same time) with DeleteBehaviour.Restrict in the context , but nothing seems to work , when i deleted a movie it worked well , but when i tried to delete a user i got this error in SSMS :
The DELETE statement conflicted with the REFERENCE constraint "FK_Review_User_UserId". The conflict occurred in database "MovieDb", table "dbo.Review", column 'UserId'.
I think i understand the problem but i cant find a solution.