zoukankan      html  css  js  c++  java
  • 解决EF一对一或多对一的删除

    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 中去执行。

  • 相关阅读:
    TCP协议
    各相机品牌型号分类
    思科华为命令对比
    网工笔记(一)
    数学笔记
    word快捷键汇总
    请个假
    word笔记
    ScrollView不能到顶部的解决方法
    Gridview 显示成正方形
  • 原文地址:https://www.cnblogs.com/fej121/p/3373273.html
Copyright © 2011-2022 走看看