I am developing a REST API in .net core 3.1
I hava a Entity Shopping Cart Item:
public class ShoppingCartItem
{
[Key]
public int Id { get; set; }
[Column("ticket_id")]
public int? TicketId { get; set; }
[Column("shopping_cart_id")]
public int ShoppingCartId { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
[Column("updated_at")]
public DateTime UpdatedAt { get; set; }
[ForeignKey(nameof(TicketId))]
public Ticket Ticket { get; set; }
[ForeignKey(nameof(ShoppingCartId))]
public ShoppingCart ShoppingCart { get; set; }
public ShoppingCartItem() { }
}
}
I am using EF core as ORM, so I created my migrations, but when I try to update the DataBase the next error appears
Introducing FOREIGN KEY constraint 'FK_ShoppingCartItem_Tikcet_ticket_id' on table 'ShoppingCartItem' 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.
By researching I have added the next lines to the DataContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ShoppingCartItem>().HasOne<Ticket>(t => t.Ticket).WithMany().HasForeignKey(s => s.TicketId).OnDelete(DeleteBehavior.Restrict);
}
But the error persist, I am not pretty sure if this is correct (I am a noob in this FrameWork). Do you have any idea what I am doing wrong?
This is my Ticket Entity:
public class Ticket
{
[Key]
public int Id { get; set; }
[Column("folio")]
public string Folio { get; set; }
[Column("status")]
public string Status { get; set; }
[Column("is_winner")]
public bool IsWinner { get; set; }
[Column("custom_name")]
public string CustomName { get; set; }
[Column("contestant_id")]
public int ContestantId { get; set; }
[Column("contest_id")]
public int ContestId { get; set; }
[ForeignKey(nameof(ContestantId))]
public Contestant Contestant { get; set; }
[ForeignKey(nameof(ContestId))]
public Contest Contest { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
[Column("updated_at")]
public DateTime UpdatedAt { get; set; }
public Ticket() { }
}