惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
B
Blog RSS Feed
GbyAI
GbyAI
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
D
Docker
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
美团技术团队
The Register - Security
The Register - Security
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
量子位
B
Blog
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
L
LangChain Blog
Latest news
Latest news
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
T
Tenable Blog
T
Tor Project blog

博客园 - 天际翔龙

一个苹果证书如何多次使用——导出p12文件[多台电脑使用] Log4net系列一:Log4net搭建之文本格式输出【转】 SMMS 2016 啟用深色主題 c#代碼小集 ZenCoding[Emmet]語法簡介【轉】 vscode: Visual Studio Code 常用快捷键【轉】 C#枚举Enum[轉] Android 杂记 解决genymotion-arm-translation.zip无法拖拽安装的问题[转] c#同步調用異步(async)方法【記錄用】 DDD基本概念 server2012/win8 卸载.net framework 4.5后 无法进入系统桌面故障解决【转】 git常用命令备忘录 MSSQL日誌傳輸熱備份注意事項 c#生成唯一编号方法记录,可用数据库主键 唯一+有序 Angular 隨記 使用dumpbin命令查看dll导出函数及重定向输出到文件【轉】 UML类图与类的关系详解【转】 知識隨記
Entity Framework中AutoDetectChangesEnabled為false時更新DB方法
天际翔龙 · 2017-09-26 · via 博客园 - 天际翔龙

Entity Framework初始化時執行:

Configuration.AutoDetectChangesEnabled = false;

會將數據庫變為NotTrack模式,也就是不會自動同步对象与其属性的状态。

這時候如果將數據表中的某行數據修改,並執行SaveChanges(),會發現數據庫中的數據並沒有保存修改後值。

解決方法有以下幾種:

  1. 通過System.Data.Entity.Infrastructure.DbEntityEntry,然後直接修改數據行的狀態,此方法會將所有表中所有列數據全部update,不推薦
    System.Data.Entity.Infrastructure.DbEntityEntry entryObj = dbContext.Entry<TableName>(tableObj);
                                entryObj.State = System.Data.Entity.EntityState.Modified;
  2. 通過System.Data.Entity.Infrastructure.DbEntityEntry對象的Property方法取得字段,然後設置CurrentValue屬性
    System.Data.Entity.Infrastructure.DbEntityEntry entryObj = dbContext.Entry<TableName>(tableObj);
                                entryObj.Property(t => t.FieldName).CurrentValue = NewValue;
  3. 通過第三方EntityFramework.Utilities包,執行AttachAndModify方法後,針對修改字段執行Set方法
    dbContext.AttachAndModify(tableObj)
                               .Set(x => x.FieldName, NewValue);
  4. 通過第三方Z.EntityFramework.Plus包,執行Update的擴展方法【多用於批次修改數據】
    dbcontext.tableName
                        .Where(p => ....)
                        .Update(p => new TableName() { FieldName = NewValue });