//
////1
public Task ReservedQuantity(long productId, long skuId, int reservedQuantity, long userId) {
return Update<Inventory>(i => new Inventory {
ReservedQuantity = i.ReservedQuantity + reservedQuantity,
AvaliableQuantity = i.AvaliableQuantity - reservedQuantity,
LastModifiedTime = DateTimeOffset.Now,
LastModifiedUserId = userId
}, i => i.SkuId == skuId); } protected Task<bool> Add<TModel>(TModel entity, Expression<Func<TModel, bool>> existsChecker = null) where TModel : class, IEntity {
return _context.Add(entity, existsChecker); } protected Task<int> Update<TModel>(Expression<Func<TModel, TModel>> updateProperties, Expression<Func<TModel, bool>> @where) where TModel : class, IEntity {
return _context.Update(updateProperties, @where); } protected Task<int> Delete<TModel>(Expression<Func<TModel, bool>> @where) where TModel : class, IEntity {
return _context.Delete(@where); }
--//2 public void Update(T obj, params Expression<Func<T, object>>[] propertiesToUpdate)
{
Context.Set<T>().Attach(obj); foreach (var p in propertiesToUpdate)
{
Context.Entry(obj).Property(p).IsModified = true;
}
}
And then to call, for example: public void UpdatePasswordAndEmail(long userId, string password, string email)
{
var user = new User {UserId = userId, Password = password, Email = email}; Update(user, u => u.Password, u => u.Email); Save();
} public interface IRepository
{
void Update<T>(T obj, params Expression<Func<T, object>>[] propertiesToUpdate) where T : class;
} public class Repository : DbContext, IRepository
{
public void Update<T>(T obj, params Expression<Func<T, object>>[] propertiesToUpdate) where T : class
{
Set<T>().Attach(obj);
propertiesToUpdate.ToList().ForEach(p => Entry(obj).Property(p).IsModified = true);
SaveChanges();
}
} //3 public int Update(TEntity entity)
{
dbcontext.Set<TEntity>().Attach(entity);
PropertyInfo[] props = entity.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
if (prop.GetValue(entity, null) != null)
{
if (prop.GetValue(entity, null).ToString() == "&nbsp;")
dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
}
}
return dbcontext.SaveChanges();
}

EF-局部更新的更多相关文章

  1. WebApiClient的JsonPatch局部更新

    1. 文章目的 随着WebApiClient的不断完善,越来越多开发者选择WebApiClient替换原生的HttpClient,本文将介绍使用WebApiClient来完成JsonPatch提交的新 ...

  2. 通过Solrj实现对索引库中数据的局部更新操作

    for (UpdateIndexDTO updateIndexDTO : data) { // 局部更新 SolrInputDocument doc = new SolrInputDocument() ...

  3. .Net页面局部更新的思考

    最近在修改以前做的模块,添加一个新功能.整理了下才发现重用率很低,大部分的东西还是需要重新写.功能里用到了局部更新,所有整理一下一路来实现局部更新的解决方案及改进. 我接触的项目开发大多是以Asp.n ...

  4. 【SSH网上商城项目实战28】使用Ajax技术局部更新商品数量和总价

         转自: https://blog.csdn.net/eson_15/article/details/51487323 昨天把项目部署了一下,玩了玩,今天完善了一下购物车中修改商品数量就能局部 ...

  5. winform 实现局部更新(如ajax实现)而整个界面不产生闪烁的解决方案

    转自原文winform 实现局部更新(如ajax实现)而整个界面不产生闪烁的解决方案 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET ...

  6. Elasticsearch之更新(全部更新和局部更新)

    前面的基础, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 Elasticsearch之curl创建索引库和索引时注意事项 Elasticsearch之 ...

  7. Solr 18 - 通过SolrJ局部更新Solr中的文档 (原子操作、非覆盖操作)

    目录 1 需求分析 2 需求实现 2.1 pom.xml依赖 2.2 Java代码示例 3 补充说明 3.1 关于文档中_version_的取值说明 3.2 store=true/false的区别 1 ...

  8. EF指定更新字段

    使用EF做更新时,若没有进行跟踪会默认全字段更新,那怎么做到只更新我们想要更新的字段呢? /// <summary> /// 修改指定属性的单条数据 /// </summary> ...

  9. ASP.Net Core使用Ajax局部更新

    由于目前ASP.NET Core中没有提供Ajax帮助器,所以参照 上一篇帖文,使用data-ajax-*属性来使用jQuery Unobtrusive Ajax功能实现HTML的局部页面元素更新. ...

  10. .NET 云原生架构师训练营(模块二 基础巩固 EF Core 更新和迁移)--学习笔记

    2.4.6 EF Core -- 更新 状态 自动变更检测 不查询删除和更新 并发 状态 Entity State Property State Entity State Added 添加 Uncha ...

随机推荐

  1. <2013 06 29> In Deutschland. Thinking in Mechanism, EE, CS, etc.

    一 在德国近一个月了,主要去了慕尼黑周边的几个景点,天鹅城堡啊,国王湖啊,然后就是在市区逛,玛丽安广场,伊萨河,英国公园,德意志博物馆... ... 总体的印象是非常好的,只是自己不怎么懂德语,但这里 ...

  2. dev 小问题列表

    1. MemoEdit > Lines Text lines are separated by line feed and carriage return characters ("\ ...

  3. CentOS 7.4系统优化/安装软件

    源改为国内源 阿里云yum源 https://opsx.alibaba.com/mirror 清华yum源 https://mirrors.tuna.tsinghua.edu.cn/ 网易163yum ...

  4. (4.4)dbcc checkdb 数据页修复

    转自:http://blog.51cto.com/lzf328/955852 三篇 一.创建错误数据库 以前看Pual写过很多数据恢复的文章,他很多的测试都是自己创建的Corrupt数据库,其实我们自 ...

  5. driver.close()和driver.quit()

    driver.close()关闭当前窗口 driver.quit()退出驱动关闭所有窗口 from selenium import webdriver from time import sleep d ...

  6. OpenGL学习进程(8)第六课:点、边和图形(三)绘制图形

    本节是OpenGL学习的第六个课时,下面介绍OpenGL图形的相关知识:     (1)多边形的概念: 多边形是由多条线段首尾相连而形成的闭合区域.OpenGL规定,一个多边形必须是一个“凸多边形”. ...

  7. sql两张表关联更新字段

    记录,推荐使用第三种,个人觉得比较好 --第一种写法,之前一直用,没有在意 UPDATE sr_t_TCodeUserReciveCfg SET fUserID=a.fUserID,fImportFl ...

  8. 跨平台移动开发_Android 平台使用 PhoneGap 方法

    PhoneGap  下载地址http://phonegap.com/install/    1.打开 Eclipse,在文件菜单下面点击 创建> Android Application Proj ...

  9. 六、golang中的结构体和方法、接口

    结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...

  10. 20145240《Java程序设计》第一周学习总结

    20145240 <Java程序设计>第一周学习总结 教材学习内容总结 第一周学习Java首先从最基本的下载.配置环境.了解基本人机命令行方式等基础知识,以及编写简单的"Hell ...