Note that there are some explanatory texts on larger screens.

plurals
  1. POMS SQL Server 2008 Check Constraint, and LINQ-To-SQL
    text
    copied!<p>This is a tough question to word...so bear with me.</p> <p>I have two tables in my database, <code>[Item]</code> and <code>[AssignedProperty]</code>. <code>[AssignedProperty]</code> is the child in a parent-child relationship with <code>[Item]</code>. So it basically just has an ItemID field and a foreign key relationship utilizing it. <code>[AssignedProperty]</code> also has an identical relationship with another table named <code>[Property]</code>, and its own primary key, AssignedPropertyID. This creates a nice many-to-many relationship. I have a constraint however, that dictates that one <code>[AssignedProperty]</code> cannot have duplicate occurrences of ItemID and PropertyID in the same record. Therefore an <code>[Item]</code> can only have one of each <code>[Property]</code>.</p> <p>This works nicely, but with LINQ-To-SQL, theres an issue in what I think is the order of execution that conflicts with this constraint.</p> <p>To produce this error, I load up an <code>[Item]</code> in my application using LINQ-To-SQL. I then select an [<code>AssignedProperty]</code> object, from the item's <code>[AssignedProperties]</code> collection and delete it. I then create a new <code>[AssignedProperty]</code> object with the exact same ItemID and PropertyID as the one I just deleted, and add it back to the collection. When I call <code>[SubmitChanges]</code> on the DataContext, it will flag me on the constraint. If the old <code>[AssignedProperty]</code> record was deleted first, then there is no reason why the new one should produce that error. It looks like it's attempting to insert the new <code>[AssignedProperty]</code> before deleting the old one.</p> <p>How should I fix this, and in the database or my app? TIA</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload