/// <summary> /// 异步使用附带新值的实体更新指定实体属性的值,此方法不支持事务 /// </summary> /// <param name="propertyExpresion">属性表达式,提供要更新的实体属性</param> /// <param name="entities">附带新值的实体属性,必须包含主键</param> /// <returns>操作影响的行数</returns> /// <exception cref="System.NotSupportedException"></exception> public async Task <int> UpdateAsync(Expression <Func <TEntity, object> > propertyExpresion, params TEntity[] entities) { propertyExpresion.CheckNotNull("propertyExpresion"); entities.CheckNotNull("entities"); CodeFirstDbContext context = new CodeFirstDbContext(); context.Update <TEntity, TKey>(propertyExpresion, entities); bool fail; try { return(await context.SaveChangesAsync(false)); } catch (DbUpdateConcurrencyException) { fail = true; } if (fail) { TKey[] ids = entities.Select(m => m.Id).ToArray(); context.Set <TEntity>().Where(m => ids.Contains(m.Id)).Load(); context.Update <TEntity, TKey>(propertyExpresion, entities); return(await context.SaveChangesAsync(false)); } return(0); }
/// <summary> /// 使用附带新值的实体更新指定实体属性的值,此方法不支持事务 /// </summary> /// <param name="propertyExpresion">属性表达式,提供要更新的实体属性</param> /// <param name="entities">附带新值的实体属性,必须包含主键</param> /// <returns>操作影响的行数</returns> /// <exception cref="System.NotSupportedException"></exception> public int Update(Expression <Func <TEntity, object> > propertyExpresion, params TEntity[] entities) { propertyExpresion.CheckNotNull("propertyExpresion"); entities.CheckNotNull("entities"); CodeFirstDbContext context = new CodeFirstDbContext(); context.Update <TEntity, TKey>(propertyExpresion, entities); try { return(context.SaveChanges(false)); } catch (DbUpdateConcurrencyException) { TKey[] ids = entities.Select(m => m.Id).ToArray(); context.Set <TEntity>().Where(m => ids.Contains(m.Id)).Load(); context.Update <TEntity, TKey>(propertyExpresion, entities); return(context.SaveChanges(false)); } }