星空网 > 软件开发 > ASP.net

EF架构~CodeFirst自关联表的插入

回到目录

这个文章对之前EF的一个补充,对于一些自关联表的添加,如果你建立了表约束确实有这种问题,一般主键为整形自增,父ID为可空,这时,在添加时如果不为ID赋值,结果就会出错。

错误:

无法确定依赖操作的有效顺序。由于外键约束、模型要求或存储生成的值,因此可能存在依赖关系。

解决:

 [HttpPost]    public ActionResult Create(WebManageMenus entity)    {      try      {        entity.Id = -1;//自关联表要主动赋值        entity.About = "";        entity.Operator = "";        menuRepository.Insert(entity);        return RedirectToAction("Index");      }      catch      {        return View();      }    }

结果:

EF架构~CodeFirst自关联表的插入

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

易结汇 ezymoney :https://www.goluckyvip.com/tag/47645.html
易鲸 Whalet :https://www.goluckyvip.com/tag/47646.html
易鲸支付:https://www.goluckyvip.com/tag/47647.html
易境通:https://www.goluckyvip.com/tag/47648.html
易可达:https://www.goluckyvip.com/tag/47649.html
无盈利产品:https://www.goluckyvip.com/tag/4765.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
西藏旅游攻略自驾游需要多少天 去西藏旅游自驾游要多久时间:https://www.vstour.cn/a/363195.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流