//
////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. 一篇搞定spring Jpa操作数据库

    开始之前你必须在项目配置好数据库,本文使用的spring boot,相比spring,spring boot省去了很多各种对以来组件复杂的配置,直接在pom配置组件,完后会自动帮我们导入组件 < ...

  2. Java 注解入门

    1.什么是注解 注解的语法: @注解名称; 注解的作用: 用来替代 xml 配置文件; 在 Servlet 3.0 中就可以使用注解来代替配置文件; 注解是由框架来读取使用的; 所有的注解都是 Ann ...

  3. setlocale同mbstowcs函数的关系(VS2008下setlocale(LC_ALL, "chs")可以执行成功,BCB使用setlocale(LC_ALL, "Chinese (Simplified)_People's Republic of China"),linux上locale别名表大概在 /usr/lib/X11/locale/locale.alias)

    序中,如果要将ASCII码字符串转换为宽字符(Unicode),可以利用标准C的mbstowcs函数. 微软在MSDN中有示例,如下: 然而,这段代码在处理含有汉字的字符串时就会出现问题.比如将: w ...

  4. PyNN:神经网络模拟器的通用接口

    PyNN:神经网络模拟器的通用接口 计算神经科学已经产生了用于模拟神经元网络的多样化软件,同时具有消极和积极的后果.一方面,每个模拟器都使用自己的编程或配置语言,导致将模型从一个模拟器移植到另一个模拟 ...

  5. 如何删除github中的仓库?

    使用Github管理项目确实有些好处,但删除仓库(repositories)确实不太好找到. 首先进入要删除的仓库,点击右下角的“settings” 然后拉到页面最下面在danger zone 按“d ...

  6. MySQL中的DDL,DML

    MySQL中的DDL,DMLDDL:数据定义语言:    CREATE,ALTER,DROP        DB组件:数据库.表.索引.视图.用户.存储过程.存储函数.触发器.事件调度器等    CR ...

  7. 算法题14 小Q歌单,牛客网,腾讯笔试题

    算法题14 小Q歌单,牛客网,腾讯笔试题 题目: 小Q有X首长度为A的不同的歌和Y首长度为B的不同的歌,现在小Q想用这些歌组成一个总长度正好为K的歌单,每首歌最多只能在歌单中出现一次,在不考虑歌单内歌 ...

  8. nodejs socket server 强制关闭客户端连接

    nodejs socket server 强制关闭客户端连接: client.destroy()

  9. LeetCode:旋转链表【61】

    LeetCode:旋转链表[61] 题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5- ...

  10. php备份mysql数据库

    <?php /*程序功能:mysql数据库备份功能*/ ini_set('max_execution_time','0'); ini_set('memory_limit','1024M');// ...