我有一个具有很多属性的对象.一堆这些大对象将被插入到数据库中,但只有一个属性在更改.将要更改的属性不是主键.第一次SaveChanges成功,但后续的失败,"ObjectStateManager中已存在具有相同键的对象.....".这是代码中的流程:
//create the entity and set the properties that don't change
TheLargeObject obj = new TheLargeObject();
obj.Prop1 =
obj.Prop2 =
...
obj.Prop20 =
//create a list of values that differ between each entity
List<int> validIds = new List<int>();
private static void SaveToDatabase(TheLargeObject obj, List<int> validIds)
{
foreach (int id in validIds)
{
//this is the only property that changes
obj.KeyId = id;
//make a copy - do we really need this?
TheLargeObject newobj = new TheLargeObject();
newobj = obj;
using(Entities …Run Code Online (Sandbox Code Playgroud)