views:

16

answers:

1

I am trying to delete an object that sometimes has a many-to-many relation. The code I use now is:

db.DeleteObject(registeredDevice);
db.SaveChanges();

This ofcourse just removes the registeredDevice. But usually this device has a many-to-many relation to a project in the database. When trying to delete the device in that scenario it will give an error.

The DELETE statement conflicted with the REFERENCE constraint

What I need to do is remove the device and its relation (the entry in the many-to-many table, not the project it is related to). How do In do this with LINQ ?

A: 

To remove everything, you need to make sure all the data is loaded. Basically, linq to sql has to know about the data it is deleting before hand to make the best judgement on how to delete it.

Hoep that helps

Nicholas Mayne
Yes you are right. But my problem isn't solved yet. What is does now is try and reset the deviceId in the ProjectDevice table to null. I need to to completely remove the record. So that would be the primary key and the 2 foreign keys (DeviceId and ProjectId). After your suggestion ive added this code btw: registeredDevice.ProjectDevice.Load();
Prd