Linq to sql add/update in different methods with different datacontexts

Posted by Kurresmack on Stack Overflow See other posts from Stack Overflow or by Kurresmack
Published on 2010-03-18T22:32:52Z Indexed on 2010/03/18 22:51 UTC
Read the original article Hit count: 393

I have to methods, Add() and Update() which both create a datacontext and returns the object created/updated.

In my unit test I call first Add(), do some stuff and then call Update(). The problem is that Update() fails with the exception:

  System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use..

I understand the issue but want to know what to do about it? I've read a bit about how to handle multiple datacontext objects and from what I've heard this way is OK.

I understand that the entity is still attached to the datacontext in Add() but I need to find out how to solve this?

Thanks in advance

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql