回到目录

这个文章对之前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自关联表的插入

回到目录

相关文章:

  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-04
  • 2021-12-26
  • 2021-07-31
猜你喜欢
  • 2021-06-29
  • 2022-01-24
  • 2021-08-14
  • 2021-05-23
  • 2021-12-24
  • 2022-12-23
相关资源
相似解决方案