关于EF中ApplyCurrentValues和ApplyOriginalValues区别
關于EF中ApplyCurrentValues和ApplyOriginalValues區別:兩者都是編輯數據時使用。
?
??????? //
??????? // 摘要:
??????? //???? 將 System.Data.Objects.ObjectStateEntry 的 System.Data.Objects.ObjectStateEntry.CurrentValues
??????? //???? 屬性設置為與所提供對象的屬性值相匹配。
??????? //
??????? // 參數:
??????? //?? currentEntity:
??????? //???? 具有要應用于原始對象的屬性更新的已分離對象。
??????? //
??????? // 返回結果:
??????? //???? 已更新的對象。
public TEntity ApplyCurrentValues(TEntity currentEntity);
參數currentEntity的取值方式有兩種
(1)從內存中查出來的對象,編輯你需要編輯的字段,然后傳入。
var user = db.Users.Where(m => m.ID== ID).FirstOrDefault();
user.Age = 10;
user.Sex="M";
db.CreateObjectSet<Users>().ApplyCurrentValues(user);
db.SaveChanges() ;
(2)用new 關鍵字創建的新對象,此處注意,創建的對象必需滿足數據表約束,然后傳入。
?db.CreateObjectSet<Users>().ApplyCurrentValues(new Users(){
ID = ID/*注意:此處的ID必需是數據庫中存在的*/,
Age=10,
Sex="M"
});
db.SaveChanges() ;
?
?
??????? //
??????? // 摘要:
??????? //???? 將 System.Data.Objects.ObjectStateEntry 的 System.Data.Objects.ObjectStateEntry.OriginalValues
??????? //???? 屬性設置為與所提供對象的屬性值相匹配。
??????? //
??????? // 參數:
??????? //?? originalEntity:
??????? //???? 具有要應用于原始對象的屬性更新的已分離對象。
??????? //
??????? // 返回結果:
??????? //???? 已更新的對象。
public TEntity ApplyOriginalValues(TEntity originalEntity);
參數originalEntity的取值方式只有一種
(1)從內存中查出來的對象,編輯你需要編輯的字段,然后傳入。
var user = db.Users.Where(m => m.ID== ID).FirstOrDefault();
user.Age = 10;
user.Sex="M";
db.CreateObjectSet<Users>().ApplyCurrentValues(user);
db.SaveChanges() ;
轉載于:https://www.cnblogs.com/yanglongv/p/3913158.html
總結
以上是生活随笔為你收集整理的关于EF中ApplyCurrentValues和ApplyOriginalValues区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 翡润年华-毛货展示003
- 下一篇: Windows 7 操作系统核心文件