Delete a many-many object from an entity structure?

I have two objects (class, student) in a database with many for many relationships (using a simple join table). I figured out how to correctly add new objects to tables, and now I would like to delete the object.

I tried the following:

// (a classobj with id==1 does exist)
ClassObj cl = (from c in entities.ClassObjs where c.ClassID == 1 select c).First();
entities.ClassObjs.DeleteObject(cl);
entities.SaveChanges();

Which gives an error:

"The DELETE statement conflicted with the REFERENCE constraint \"FK_JunctionClassObjsStudents_Students\".

Where JunctionClassObjsStudents is the name of the join table that creates the relationship between many and many relationships between classes and student tables.

What do I need to do? Thank you for your help!!

+3
source share
3 answers

( SET NULL) FK, .

+2

StudentObj, ClassObj . .

0

, ( )...

→ Class_Student_Junction < -

, . - , , . , ...

1: ,

2: , 1.

Step 3: Remove the class entry from the Classes table. There should be no violations in this order.

0
source

Source: https://habr.com/ru/post/1758097/


All Articles