Why when I delete or edit table record x is also removed from table y?

5

I have a system in ASP MVC with C # entity .

I have the table Pedidos and Agenda .

In the agenda table I have a column with id of the request.

When the request is canceled, I have to remove from the Agenda table to release a time for a new request.

The problem is that when you remove the row from the Agenda table with the request 13 (for example), the pedido table is also removed.

I've already done a search and found something about Cascade in SQL , but this is not enabled.

What can it be?

Follow the code below:

 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);                        
    }
}
    
asked by anonymous 04.11.2016 / 15:29

2 answers

4

This is called Cascade Delete and is a default behavior of the Entity Framework.

You can also put some general rules, not just specific ones for each model.

Ex:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
    modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()
}

So you remove in all models.

    
04.11.2016 / 15:56
2

example to remove cascade delete

public class SchoolContext<: DbContext
{
    public SchoolContext():base("MySchool")
    {
                }

    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>()
            .HasOptional<Standard>(s => s.Standard)
            .WithMany()
            .WillCascadeOnDelete(false);
    }
}

source: link

    
04.11.2016 / 15:52