© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
Stan

❔ Error adding new model in EF Core

I'm trying to add a new model "Tickets" in my ASP.NET Core web API. After trying to update the database with a migration, it slaps me in the face with
Introducing FOREIGN KEY constraint 'FK_Tickets_Zitplaatsen_ZitplaatsId' on table 'Tickets' 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.
Introducing FOREIGN KEY constraint 'FK_Tickets_Zitplaatsen_ZitplaatsId' on table 'Tickets' 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.


These are the (hopefully) relevant currently existing models I have:

public class Ticket
{
    public int Id { get; set; }
    
    public Uitvoering? Uitvoering { get; set; }
    public Zitplaats? Zitplaats { get; set; }
    public IdentityUser? Klant { get; set; }
}
public class Ticket
{
    public int Id { get; set; }
    
    public Uitvoering? Uitvoering { get; set; }
    public Zitplaats? Zitplaats { get; set; }
    public IdentityUser? Klant { get; set; }
}


public class Zitplaats
{
    public int Id { get; set; }
    
    public int X { get; set; }
    public int Y { get; set; }
    
    public int Rang { get; set; }
    
    public Zaal? Zaal { get; set; }
    
    public List<Ticket> Tickets { get; set; }
}
public class Zitplaats
{
    public int Id { get; set; }
    
    public int X { get; set; }
    public int Y { get; set; }
    
    public int Rang { get; set; }
    
    public Zaal? Zaal { get; set; }
    
    public List<Ticket> Tickets { get; set; }
}


public class Zaal
{
    public int Id { get; set; }
    public List<Zitplaats> Zitplaatsen { get; set; }
    public List<Uitvoering> Uitvoeringen { get; set; }
}
public class Zaal
{
    public int Id { get; set; }
    public List<Zitplaats> Zitplaatsen { get; set; }
    public List<Uitvoering> Uitvoeringen { get; set; }
}


public class Uitvoering
{
    public int Id { get; set; }
    
    public DateTime StartTijd { get; set; }
    
    public Voorstelling Voorstelling { get; set; }
    public Zaal? Zaal { get; set; }
    
    public List<Ticket> Tickets { get; set; }
}
public class Uitvoering
{
    public int Id { get; set; }
    
    public DateTime StartTijd { get; set; }
    
    public Voorstelling Voorstelling { get; set; }
    public Zaal? Zaal { get; set; }
    
    public List<Ticket> Tickets { get; set; }
}


I've looked around and tried adding this in my OnModelCreating:

        builder.Entity<Ticket>()
            .HasOne(x => x.Zitplaats)
            .WithMany(x => x.Tickets)
            .HasForeignKey(x => x.ZitplaatsId)
            .OnDelete(DeleteBehavior.Restrict);
        builder.Entity<Ticket>()
            .HasOne(x => x.Zitplaats)
            .WithMany(x => x.Tickets)
            .HasForeignKey(x => x.ZitplaatsId)
            .OnDelete(DeleteBehavior.Restrict);


which gave the same error. Any help would be appreciated :)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Adding EF Core Migration Error
C#CC# / help
4y ago
EF core error
C#CC# / help
2y ago
❔ Adding database entries with EF Core
C#CC# / help
3y ago
EF Core Model not-required property
C#CC# / help
4y ago