是否存在支持过滤部分更新的EF扩展?

时间:2015-07-22 13:23:40

标签: c# entity-framework

要更新实体,我需要查询它并更改其属性或附加修改后或附件后更改。

但我真正需要的是:

context.MyEntities.UpdateWhere( 
    entity => { entity.IsDeleted = true; entity.DeletedDate = today},
    entity => entitiesToDelete.Contains(entity.id) && !entity.IsDeleted));

我希望发出类似的(参数化的)SQL:

update MyEntitiesTable
set IsDeleted = 1, DeletedDate = @today
where id in (...ids list...) and IsDeleted = 0;

1 个答案:

答案 0 :(得分:2)

EntityFramework.Extended具有批量更新和删除功能。 GitHub

您的问题的示例语法将如下:

context.MyEntities.Where(entity => entitiesToDelete.Contains(entity.id) && !entity.IsDeleted)
        .Update(t => new MyEntity { entity.IsDeleted = true, entity.DeletedDate = today });