我很好奇一次更新实体列表的正确方法是什么.
public ActionWas Update(IEnumerable<surcharge_template> templates)
{
try
{
var templatesToBeUpdated = _context.surcharge_template.Where(x => templates.Select(template => template.st_key).Contains(x.st_key));
//Right here I need to map all of the differences from the original templates
//to the new templates
_context.Entry(templatesToBeUpdated).State = EntityState.Modified;
}
catch (Exception ex)
{
return _exceptionConverter.Convert(ex);
}
_context.SaveChanges();
return ActionWas.Successsful;
}
Run Code Online (Sandbox Code Playgroud)
我发表评论,我不知道如何处理这个问题.我从数据库中获取原始模板,然后我需要映射它们,然后提交并保存.
那么映射它们的正确方法是什么?
更新:
我想用EF只对数据库进行一次调用.枚举列表将导致多重更新语句.
您不需要映射当前实体。您可以改用 SQL Update 等语法。
使用 EntityFramework.Extend
//update all tasks with status of 1 to status of 2
context.Templates.Update(
t => t.StatusId == 1,
t2 => new Template {StatusId = 2});
Run Code Online (Sandbox Code Playgroud)
这只是一种解决方法,最后如果您需要对每个模板进行不同的更改,您将需要修改实体状态。
| 归档时间: |
|
| 查看次数: |
226 次 |
| 最近记录: |