Consider this typical disconnected scenario:
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
Sub-Optimal Workarounds
This has to do with the fact that your datacontext (db) cannot track the same entity more than once. See this post for more details on what's going on.
One of the obscure comments at the bottom of that post says to try:
public void Update(Customer customer)
{
NorthwindDataContext context = new NorthwindDataContext();
context.Attach(customer);
context.Refresh(RefreshMode.KeepCurrentValues, customer);
context.SubmitChanges();
}
Let me know how it works out for you, as the OP of that post says it worked out for him...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With