C
C#2y ago
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 :)
3 Replies
phaseshift
phaseshift2y ago
What are the options for DeleteBehavior? should be something closer to 'do nothing' than 'restrict'
Stan
Stan2y ago
just tried DeleteBehavior.NoAction which gave the same error might be worth noting, this is what my entire OnModelCreating looks like:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Ticket>()
.HasOne(x => x.Zitplaats)
.WithMany(x => x.Tickets)
.HasForeignKey(x => x.ZitplaatsId)
.OnDelete(DeleteBehavior.NoAction);
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Ticket>()
.HasOne(x => x.Zitplaats)
.WithMany(x => x.Tickets)
.HasForeignKey(x => x.ZitplaatsId)
.OnDelete(DeleteBehavior.NoAction);
}
I've tried placing the base.OnModelCreating(builder); which led to the same error Also tried removing base.OnModelCreating(builder); but that gave me this error: The entity type 'IdentityUserLogin<string>' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. After dropping all tables and trying the migration again, everything seems to work as intended
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Error message when trying to move direction for my tiles in a N-puzzleHi! I get this error message when trying to move around my tiles in my N puzzle "System.IndexOutOfRa❔ I should develop an app for windows and macos but i don't know how to do thisHello, today i started my first internship, but in my company i m the only dev and as a project i ha❔ any way to add panel to winforms net core?I am beginner and I was wondering if there was a way to add panel to net core project, cause it is n❔ Copying code from youtube and it doesn't work for meI'm new to the C# scripts and i am trying to make a board game, while trying to understand how to do❔ Error while trying to connect to MySql using MySql .Data**While trying to connect to maria db using mysql.data i get this error: ** > `Unhandled task excep❔ ✅ 'ConsoleColor' does not contain a definition for 'DrawingColor'I am experimenting a sample code to create a colourful UI on console, and am getting this error: ```❔ WinUI exe launch issueI receive this following issue every time from event viewer when I try to start the application via ✅ Logging incoming HTTP requests with WebApplicationI've got a route in my app that isn't being triggered properly. I can't currently figure out what's ❔ What unit tests should I create for my ASP.NET API Controller / project?I am new to Unit Testing. I have watched a video on XUnit and FluidAssertions, but I am unsure as to✅ Blocking ASP.NET Core Shutdown fullyWhen i shut down my ASP.NET Core API i have to make 0-n API requests and receive (and answer) that m