Note that there are some explanatory texts on larger screens.

plurals
  1. POMS SQL Server 2008 Check Constraint, and LINQ-To-SQL
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COIs ItemID, PropertyID your primary key in your AssignedProperty table? Is so, can you explain why are you deleting and re-inserting the exact same record? The fix is simple though: just submit changes two times, after deleting and after re-inserting
      singulars
    2. COAhh, if I had to forget something it would be that, lol. AssignedProperty has its own Primary Key, AssignedPropertyID, autoincrement. As far as the item, the user is allowed to make as many changes to the Item entity as they like, and save or reject the changes at any time. So the way the app tries to handle this situation is such that all the changes can be submitted at the same time. Ideally, the user probably wouldn't delete an AssignedProperty and make the exact same one again. But its possible.
      singulars
    3. COThe only way I think it could be done is that your app detects when a AssignedProperty is deleted then reinserted so it doesn't delete it in the first place. I don't think it's a good idea to remove the constraint from the db because sooner or later the data can be corrupted. I don't know any way you can predict the way linq2sql handles all the transactions, or force an order in doing so, therefore it could be a very difficult approach too.
      singulars
 

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