-2

I have a SQL Server database like this: master table, details table, subdetails table. And I use Linq and EF 4.0.

So, master can have more details and each detail can have more subdetails.

I have a MASTER ID and I want delete this record.

How can I do to delete a master record with details and subdetails in un colpo solo (EF4.0)?

Roby G.
  • 95
  • 1
  • 1
  • 9

1 Answers1

2

EF enables cascading delete effect by default for all the entities.

The following is copied from here.

Consider the following Student and Standard entities that have one-to-many relationship.

public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

    public virtual Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    {
        Students = new List<Student>();
    }
    public int StandardId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

The following example demonstrates cascade delete effect between entities that have one-to-many relationship.

using( var ctx = new SchoolContext() ) {

   var student1 = new Student() { StudentName = "James" };
   var student2 = new Student() { StudentName = "Gandhi" };

   var standard1 = new Standard() { StandardName = "Standard 1" };

   student1.Standard = standard1;
   student2.Standard = standard1;

   ctx.Students.Add( student1 );
   ctx.Students.Add( student2 );

   //inserts students and standard1 into db
   ctx.SaveChanges();

   //deletes standard1 from db and also set standard_StandardId FK column in Students table to null for
   // all the students that reference standard1.
   ctx.Standards.Remove( standard1 );

   ctx.SaveChanges();
}

In the above example, it deletes standard1 from db and also set standard_StandardId FK column in Students table to null for all the records that reference standard1.

EF automatically deletes related records in the middle table for many-to-many relationship entities if one or other entity is deleted.

Thus, EF enables cascading delete effect by default for all the entities.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • thks for reply.. but I have three relation, not two. I want to delete master, details and subdetails.. – Roby G. Jan 12 '17 at 07:36
  • @user2080358 mechanics of master-details relatiion apply to detail-subdetails relation aswell, don't you see? – Diligent Key Presser Jan 12 '17 at 08:42
  • @user2080358 doesn't matter. Once parent is deleted, children, grand children, their children and on and on... All will be deleted. It is a cascading effect – CodingYoshi Jan 12 '17 at 13:51
  • @CodingYoshi: But I have to put OnDelete property association = Cascade? – Roby G. Jan 12 '17 at 16:44
  • @user2080358 No you do not. EF does that by default. If you do not want it, then you need to disable it. If you want to disable it, read the article I linked in my answer. – CodingYoshi Jan 12 '17 at 16:52
  • @CodingYoshi: thks for reply, but I have a problem: I use EF4.0, so, I don't see Remove or RemoveRange in list.. – Roby G. Jan 12 '17 at 17:19
  • @user2080358 use `DeleteObject` method instead. So for the above code in my answer it will be `ctx.Standards.DeleteObject(standard1);` – CodingYoshi Jan 12 '17 at 17:23
  • @CodingYoshi thks but when I try this I get this message: Message = "The DELETE statement conflicted with the REFERENCE constraint \"FK_Details_Master\". The conflict occurred in database .... It's normal, I can't to delete row with reference between them.. How can cascanding work? thks – Roby G. Jan 12 '17 at 17:37
  • You did not mention EF 4 in your question so I did not know that. But for EF 4 I had to dig and find you the documentation. Please read them carefully and apply it to your situation as described [here](https://msdn.microsoft.com/en-us/library/bb738695(v=vs.100).aspx) – CodingYoshi Jan 12 '17 at 17:53
  • @CodingYoshi : you are right.. I forgot.. I was focused to solve my problem.. thanks for link .. – Roby G. Jan 12 '17 at 20:06