将几个现有的修改权限附加到上下文

时间:2016-02-11 13:48:31

标签: c# entity-framework

做这样的事情的最佳方式是什么:

var existingEntities = new [] 
{ 
      new Product {Name = "Name1", Count = 10}, 
      new Product {Name = "Name2", Count = 20}
}; 

using (var context = new ProductContext()) 
{ 
    // attach several entities

    context.SaveChanges(); 
}

我应该迭代每个实体并将其设置为State = EntityState.Modified?

1 个答案:

答案 0 :(得分:0)

是的,迭代并设置

context.Entry(existingproduct).State = EntityState.Modified;

是要走的路。

来源:https://msdn.microsoft.com/en-us/data/jj592676.aspx