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
StanOP2y 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