private static void Method08() { Member member = new Member { Id = 1, Password = "******" + DateTime.Now.Second }; using (var db = new DataContext()) { //先查询一次,让上下文中存在相同主键的对象 Member oldMember = db.Members.Single(m => m.Id == 1); Console.WriteLine("更新前:{0}。", oldMember.AddDate); try { DbEntityEntry <Member> entry = db.Entry(member); entry.State = EntityState.Unchanged; entry.Property("Password").IsModified = true; } catch (InvalidOperationException) { ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectStateEntry objectEntry = objectContext.ObjectStateManager.GetObjectStateEntry(oldMember); objectEntry.ApplyCurrentValues(member); objectEntry.ChangeState(EntityState.Unchanged); objectEntry.SetModifiedProperty("Password"); } db.Configuration.ValidateOnSaveEnabled = false; int count = db.SaveChanges(); db.Configuration.ValidateOnSaveEnabled = true; Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。"); Member newMember = db.Members.Single(m => m.Id == 1); Console.WriteLine("更新后:{0}。", newMember.Password); } }
//场景三的异常处理,从ObjectContext着手处理 public void Method8() { const string userName = "******"; long id; using (var db = new DefaultDbContext()) { id = db.Set <SysUser>().Single(m => m.UserName == userName).Id; } SysUser user = new SysUser { Id = id, Password = "******" + DateTime.Now.Second }; using (var db = new DefaultDbContext()) { //关闭自动跟踪,方便调试查看 db.Configuration.AutoDetectChangesEnabled = false; //通过查询,让上下文中存在相同主键的对象 SysUser oldUser = db.Set <SysUser>().Find(user.Id); System.Console.WriteLine("更新前:{0}。", oldUser.Password); try { DbEntityEntry <SysUser> entry = db.Entry(user); entry.State = EntityState.Unchanged; entry.Property("Password").IsModified = true; } catch (InvalidOperationException) { ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectStateEntry objectEntry = objectContext.ObjectStateManager.GetObjectStateEntry(oldUser); objectEntry.ApplyCurrentValues(user); objectEntry.ChangeState(EntityState.Unchanged); objectEntry.SetModifiedProperty("Password"); } db.Configuration.ValidateOnSaveEnabled = false; int count = db.SaveChanges(); db.Configuration.ValidateOnSaveEnabled = true; System.Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。"); SysUser newUser = db.Set <SysUser>().Single(m => m.Id == id); System.Console.WriteLine("更新后:{0}。", newUser.Password); } }
public static void Update <TEntity, TKey>(this DbContext dbContext, Expression <Func <TEntity, object> > propertyExpression, params TEntity[] entities) where TEntity : EntityBase <TKey> { if (propertyExpression == null) { throw new ArgumentNullException("propertyExpression"); } if (entities == null) { throw new ArgumentNullException("entities"); } ReadOnlyCollection <MemberInfo> memberInfos = ((dynamic)propertyExpression.Body).Members; foreach (TEntity entity in entities) { DbSet <TEntity> dbSet = dbContext.Set <TEntity>(); try { DbEntityEntry <TEntity> entry = dbContext.Entry(entity); //entry.State = EntityState.Unchanged; foreach (MemberInfo memberInfo in memberInfos) { entry.Property(memberInfo.Name).IsModified = true; } } catch (InvalidOperationException) { TEntity originalEntity = dbSet.Local.Single(m => Equals(m.SysNo, entity.SysNo)); ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; ObjectStateEntry objectEntry = objectContext.ObjectStateManager.GetObjectStateEntry(originalEntity); objectEntry.ApplyCurrentValues(entity); objectEntry.ChangeState(EntityState.Unchanged); foreach (MemberInfo memberInfo in memberInfos) { objectEntry.SetModifiedProperty(memberInfo.Name); } } } }