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

推荐订阅源

S
Schneier on Security
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
IT之家
IT之家
V
V2EX
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 叶小钗
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
博客园 - 聂微东
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
H
Help Net Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
P
Palo Alto Networks Blog
T
Threatpost
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cloudbric
Cloudbric
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
量子位
Security Latest
Security Latest
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog

博客园 - 为森

NPM设置镜像 WCF 自托管、无配置文件实现jsonp(跨域)的访问 关于nodejs4.0 npm乱码以及离线全局安装时要注意的问题 C# 正则表达式 转自-每日一bo 关于async & await(TAP)异步模型的异常捕获 一个Public的字段引起的,谈谈继承中的new 解决Bootstrap 附加导航(Affix)的问题和使用时若干注意事项 一次小异常的排查,悲剧的无以复加!!! async & await 小扩展大用处,自己扩展一个ForeachRead吧 转 常用工具和技术 implicit和 explicit关键字 MVC 自定义 以表达式树为参数的htmlhelper 解决解密时出现"要解密的数据的长度无效" 或 "填充无效无法被移除" 的错误 EF 的一些不常用的功能 详细说明 配置 Sublime Text 开发node.js(windows)包括sub2和sub3的区别 sql 判断 数据库 表 字段 是否存在 转 WCF中同步和异步通讯总结 关于WCF
解决EF一对一或多对一的删除
为森 · 2013-10-17 · via 博客园 - 为森

people 类中有 zhengshu类 且是一对一,现在要删除people类中的zhengshu

网上看了N多办法,什么更新外键什么滴。

其实方法简单极了

using (KJExamEntity context = new KJExamEntity())
{
context.Peoples.Attach(people);
context.ZhengShus.Remove(people.SBZhengShu);
context.SaveChanges();
}

完事之后people.SBZhengShu的会自动为null。

万万不可

using (KJExamEntity context = new KJExamEntity())
{
context.Peoples.Attach(people);
people.SBZhengShu=null;
context.SaveChanges();
}

using (KJExamEntity context = new KJExamEntity())
{
context.ZhengShus.Attach(zs);
people.SBZhengShu=null;
context.SaveChanges();
}

总之一句话 从爹删起,微软的想法很简单,你要删儿子,爹肯定受影响 所以你要把爹和儿子放在一个context 中去执行。