5
I have a system in ASP MVC with C# using Entity framework.
I have the table Pedidos
and Agenda
.
On the table agenda
I have a column with the id
of the request.
When the order is canceled, I have to remove from the table Agenda
to release a schedule for a new order.
The problem is that when you remove the row from the table Agenda
with request 13(for example), in the table pedido
the same is removed also.
I already did a search and found something on Cascade
in the SQL, but it’s not activated.
What can it be?
Follows 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);
}
}
Thanks, just confirming: this way is deleted only from 1 table?
– Peres
But this behavior makes Entity do because he thinks the other record will be orphaned, so make sure you really need to keep the other data, of course he just 'thinks' you should decide.
– Dorathoto
Ok, thank you very much!! yes I need it removed only from 1 table, to keep order history in the system.
– Peres