解决EF一对一或多对一的删除
2013-10-17 03:33
288 查看
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 中去执行。
网上看了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 中去执行。
相关文章推荐
- java 关于操作oracle的clob类型
- 有关 C 指针的个人另类概念(1)
- Java is Pass-by-Value, Dammit!
- linux服务器安全加固shell脚本
- nodejs广播
- Best Time to Buy and Sell Stock
- 函数1(四)
- nodejs服务
- 【学习】linux shell 编程
- RHEL6.4更改为CentOS6.4的yum源
- 单链表操作大全
- 在 php 中通过 CURL 上传文件
- 一句话经典总结红黑树插入
- 小流涓涓成江海,拙技点点构平台——多线程开发
- 英文面试自我介绍
- RHEL6安装图形界面
- 白话经典算法系列之——快速排序
- java System类
- 【MZ】ZOJ 3494 BCD Code AC自动机+数位DP
- Permutations II