EF specified field update

When using the EF to do the update, if not tracked by default the whole field is updated, then how do we update only the fields you want to update it?

. 1  ///  <Summary> 
2  /// modify specific properties of the single piece of data
 . 3  ///  </ Summary> 
. 4  ///  <typeParam name = "T"> entity </ typeParam> 
. 5  ///  <param name = "context"> context </ param> 
. 6  ///  <param name = "Model"> to modify the entity information </ param> 
. 7  ///  <param name = "expression the"> specified in a modified field </ param> 
. 8  public  static  void the Modify <T> ( the this the DbContext context, Model T, the Expression <Func <T, Object >> expression)
 9     whereT: class 
10  {
 . 11      context.Update (Model);
 12 is      // 4.1 to add objects to the EF 
13 is      var entry context.Entry = <T> (Model);
 14      // 4.2 first set of objects packaged condition an Unchanged 
15      = entry.State EntityState.Unchanged;
 16      // 4.3 cycles are modified property name array 
. 17      the foreach ( var ProInfo in expression.GetPropertyAccessList ())
 18 is      {
 . 19          IF (! String .IsNullOrEmpty (proInfo.Name))
 20 is              //4.4 The state of each modified attribute to a modified state; update statement is generated later, it only updates the modified attribute 
21 is              entry.Property (proInfo.Name) = .IsModified to true ;
 22 is      }
 23 is }

Use: as follows, and the Status Update Model table fields UpdateTime 

1 _dbContext.Modify(Model, p => new
2 {
3     p.Status,
4     p.UpdateTime
5 });
6 await _dbContext.SaveChangesAsync();

Some students find that, when invoked using the _dbContext.SaveChangesAsync (), why not directly integrated into the inside methods? This will vary due to demand, if your project does not involve changes to multiple tables, and do not need to do some transactional processing, a method that is integrated into the inside no problem, but also support to do so.

Guess you like

Origin www.cnblogs.com/az4215/p/11576193.html