0

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

4
  • Have you tried to change DeleteBehavior.Restrict to DeleteBehavior.NoAction and see what resulted ? Commented Oct 3 at 12:50
  • @Charlieface EF Core, it is in the subject Commented Oct 3 at 13:04
  • @YoannBlossier yes, I posted it in the code Commented Oct 3 at 13:04
  • I found an answer, thank you for your responses Commented Oct 3 at 13:04

1 Answer 1

2

Found a solution, searched the migrations themselves and changed from Cascading to Restrict

migration.AddForeignKey(
name: "FK_Films_People_ComposerId",
column: "ComposerId",
proncipalTable: "People",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
Sign up to request clarification or add additional context in comments.

3 Comments

How did that code get generated?
Good for you but in fact it is not the solution because next time the generator will do the same :-). Do you identify why you continue having Cascade instead Restrict in migration script ?
Sounds like your migration code is stale. Perhaps you modified your code and forgot to generate a new migration? The solution is to create a new migration, not modify an older one. Otherwise the migrations will always be out of sync and possibly create such conflicts

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.