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

推荐订阅源

WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Proofpoint News Feed
量子位
K
Kaspersky official blog
S
SegmentFault 最新的问题
博客园 - 叶小钗
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
F
Fortinet All Blogs
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
T
Tenable Blog
U
Unit 42
S
Schneier on Security
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
C
Cybersecurity and Infrastructure Security Agency CISA
V
Visual Studio Blog
The Hacker News
The Hacker News
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
Intezer
Cyberwarzone
Cyberwarzone
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
T
The Exploit Database - CXSecurity.com
V
V2EX
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
PCI Perspectives
PCI Perspectives
腾讯CDC
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News | PayPal Newsroom
Recorded Future
Recorded Future
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
C
Check Point 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 });