I have the following entities
Film:
using System.ComponentModel.DataAnnotations.Schema;
namespace MyWebAPIApplication.Entities
{
public class Film : Base
{
string Title { get; set; }
public int ReleaseYear { get; set; }
public string Synopsis { get; set; }
public int DurationMinutes { get; set; }
public int CountryId { get; set; }
[ForeignKey("CountryId")]
public Country CountryProduced { get; set; }
public int ProducerId { get; set; }
[ForeignKey("ProducerId")]
public Person Producer { get; set; }
public int DirectorId { get; set; }
[ForeignKey("DirectorId")]
public Person Director { get; set; }
public int ScreenwriterId { get; set; }
[ForeignKey("ScreenwriterId")]
public Person Screenwriter { get; set; }
public int ComposerId { get; set; }
[ForeignKey("ComposerId")]
public Person Composer { get; set; }
public ICollection<Person> Actors { get; set; }
public ICollection<Trivia> Trivias { get; set; }
public ICollection<FilmConnection> FilmConnections { get; set; }
public Review Review { get; set; }
}
}
Person
namespace MyWebAPIApplication.Entities
{
public class Person : Base
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public DateTime? DateOfBirth { get; set; }
public int? FilmId { get; set; }
public Film Film { get; set; }
public int? CountryId { get; set; }
public Country CountryBorn { get; set; }
public ICollection<Trivia> Trivias { get; set; }
}
}
Here is my FilmDBContext
public class FilmDBContext : DbContext
{
public FilmDBContext(DbContextOptions<FilmDBContext> options)
: base(options)
{
}
public DbSet<Film> Films { get; set; }
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Film>()
.HasOne(f => f.Composer)
.WithMany()
.HasForeignKey(f => f.ComposerId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Film>()
.HasOne(f => f.Producer)
.WithMany()
.HasForeignKey(f => f.ProducerId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Film>()
.HasOne(f => f.Director)
.WithMany()
.HasForeignKey(f => f.DirectorId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Film>()
.HasOne(f => f.Screenwriter)
.WithMany()
.HasForeignKey(f => f.ScreenwriterId)
.OnDelete(DeleteBehavior.Restrict);
}
}
When I run update-database I always receive that error
Introducing FOREIGN KEY constraint 'FK_Films_People_ComposerId' on table 'Films' 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.
Despite the fact I have appropriate code in the OnModelCreating event, I still receive that error. Any ideas how I could fix it?
Thank you very much in advance