15

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.

alt text

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

2 Answers 2

23

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...

Sign up to request clarification or add additional context in comments.

2 Comments

+1; thanks! That did indeed work! I'll leave this question 'unanswered' for now, but this answer definitely solves the problem!
As JustLoren says Attach is there for giving a context an entity it doesn't yet have - it can't be used to replace one. In a typical situation if you are getting an object back it is sometime later and will be attached to a new data context.
-4

Instead of creating a new context you could do this:

public void Update(Customer customer)
{
    Customer oldCustomer= db.Custs.First(c => c.ID == customer.ID);  //retrieve unedited 
    oldCustomer = customer;  // set the old customer to the new customer.
    db.SubmitChanges();  //sumbit to database
}

1 Comment

That won't update the database, it will just change what the variable oldCustomer refers to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.