Consider this typical disconnected scenario:
- load a Customer object from SQL Server using LINQ To SQL
- user edits the entity, and the presentation tier sends back the entity modified.
- the data layer, using L2S, must send the changes to SQL Server
Consider this LINQ To SQL query whose intention is to take a Customer entity.
Cust custOrig = db.Custs.SingleOrDefault(o => o.ID == c.ID); //get the original
db.Custs.Attach(c, custOrig); //we don't have a TimeStamp=True property
db.SubmitChanges();
DuplicateKeyException: Cannot add an entity with a key that is already in use.
Question
- How can you avoid this exception?
- What's the best strategy for updating an entity that does NOT have/want/need a timestamp property?
Sub-Optimal Workarounds
- manually set each property in the updated customer to the orig customer.
- spin up another DataContext