Search code examples
entity-frameworkdnsdbcontextdetach

What am I missing out? (An entity with the same identity already exists in this EntitySet.)


I am using DTOs and converters in an entity framwork scenario. I want to update an entity. So I put this statement: databasecontext.Tablename(s).Attach(entityobj); Whenever it gets here the program ends with this error: An entity with the same identity already exists in this EntitySet.

I felt I could get around this by saying databasecontext.Tablename(s).Detach(entityobj); before I called Attach again. But the program still fails on the Attach statement.

What am I missing? Which method on the DB context calls Update in the Domain Service?


Solution

  • It looks like you are keeping the context open while you make the changes.

    In that case you do not need to reattach the object to the context.

    If you open a context, get an object from a context, then close the context, make some changes to the object, then open a new context. In this case you need to attach the object to the context.