0

当我使用 add() 将实体输入到数据库时,我得到以下 SqlException(对象已存在)

Violation of PRIMARY KEY constraint '....'. 
Cannot insert duplicate key in object 'PROJ.DesignProjects'. 
The duplicate key value is ... The statement has been terminated."} 
System.Exception {System.Data.SqlClient.SqlException}

然后我修复它以在添加之前运行删除,我得到

{"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."}

那我该怎么做呢?

4

1 回答 1

0

试试 AddOrUpdate() 方法:

yourContext.yourDbSet.AddOrUpdate(yourObject);

编辑:进一步阅读http://msdn.microsoft.com/en-us/library/hh846520(v=vs.103).aspx

于 2013-10-15T20:49:46.233 回答