Question:
I have a system in ASP MVC with C# using entity framework .
I have the Pedidos
and Agenda
table.
In the agenda
table I have a column with the order id
.
When the order is cancelled, I have to remove it from the Agenda
table to release a time for a new order.
The problem is that when you remove the row from the Agenda
table with order 13 (for example), in the pedido
table it is also removed.
I've already done a search and found something about Cascade
in SQL , but that's not enabled.
What can it be?
Follow code:
public partial class ifixEntities : DbContext
{
public MyEntities()
: base("name=MyEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Entity<Agenda>().HasOptional<Pedido>(s => s.Pedido).WithMany().WillCascadeOnDelete(false);
}
}
Answer:
This is called Cascade Delete and is standard Entity Framework behavior.
You can also put some general rules, not just specific for each model.
Ex:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()
}
So you remove in all models.