从一个ID更新Entity Framework中的多行

时间:2018-07-15 05:30:03

标签: c# sql asp.net-mvc entity-framework-6

我正在尝试为Entity Framework创建一个查询,该查询将允许我使用一个id并更新与其关联的字段。

SQL示例:

UPDATE Recibo
SET Estado = 3
WHERE IdApartado IN (7)

如何将以上内容转换为实体框架?

2 个答案:

答案 0 :(得分:0)

在EF中执行in子句的两种方法:

第一个是 Contains

return dbContext.Query<R>.Where(r => subset.Contains(r.Id)).ToList();

秒是foreachcontains

 return dbContext.Query<R>.Where(r => subset.Contains(r.Id)).ToList().ForEach(a => a.id = "7");

这是因为EF中sql in的最佳等效项是Contains

答案 1 :(得分:0)

要更新数据,您需要阅读它,对其进行编辑,然后保存上下文see here

      <div class="row">
               <div class="col-sm-3>
                 <div class="form-group"> 
                  <label for="inputEmail">field</label> 
                  <input type="text" class="form-control" id="" 
                     placeholder=""> </div>     
               </div>
               <div class="col-sm-3>
               <!--i am using these four lines code in every col for input fields for sake clarity i wrote it only here.
                  <div class="form-group"> 
                  <label for="inputEmail">field</label> 
                  <input type="text" class="form-control"id="" 
                     placeholder="item desc"> </div>     
               </div>
               <div class="col-sm-3>
                
               </div>
               <!---fourth col-!>
               <div class="col-sm-3>
               </div>
               <!--fifth col--!>
               <div class="col-sm-3>
               <!-I need to align this column with the above column remeber this coloumn field does not have labels thats why I am not able to align both columns(fourth and fifth)-!>
               </div>

            </div>

但是,我想指出的是,如果您使用的是存储库模式,那么您不需要在存储库中进行保存/更新,我强烈建议您使用article about common mistakes in the Repository Pattern

  

还有另一个非常常见的错误!正如我在YouTube视频中解释的那样   之前,您的存储库不应具有Save()或Update()方法。   我重复一遍:将存储库视为域对象的集合   记忆。集合是否具有Save()或Update()方法?不!