This is a by design behavior. Once you add an entity in the context it becomes tracked and if you try to add an entity with the same primary key, regardless if you have saved or cleared the changes you will get the mentioned exception. In order to solve this issue I can suggest you the following alternatives:
- Instead of Add you can use AttachCopy
which will check if the entity is already present in the context or in the DB before adding it. You should know that this approach will cause some additional selects for checking weather an entity with this Id is present in the DB.
- You can use sort living context instances so that each add happens in separate context instance.
I hope this helps. Should you have any additional questions do not hesitate to get back to use.
OpenAccess ORM is now Telerik Data Access
. For more information on the new names, please, check out the Telerik Product Map