这个文章对之前EF的一个补充,对于一些自关联表的添加,如果你建立了表约束确实有这种问题,一般主键为整形自增,父ID为可空,这时,在添加时如果不为ID赋值,结果就会出错。
错误:
无法确定依赖操作的有效顺序。由于外键约束、模型要求或存储生成的值,因此可能存在依赖关系。
解决:
[HttpPost]
public ActionResult Create(WebManageMenus entity)
{
try
{
entity.Id = -;//自关联表要主动赋值
entity.About = "";
entity.Operator = "";
menuRepository.Insert(entity);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATAAAABaCAIAAACwgZ0sAAAMaElEQVR4nO2d3U8bVxrG/U+gXKx6Ga4qGWn+gZW5yDW1a5SwliqrqqquuhISKXtRRdHuspIDO23WraOkTUPTfEGyKm7jKNsoTSibYsd8GQyOEzBJGgeS3ToQKBhb9uzFfPjMzBl78Ac+Ns+rnxDY8/EyOs+855yZecYiCKkivPHG74ovAACoIpa6ZwAAUCghSFRIAPYSVEgAGAIVEgCGqEiQx02EIAh1/ydJEtNXE5FriZmri1NDjyYvPQgNzt87Mzv6z6lb/RM3+7ZeTSCxRkmsKalUkL8vGtGZIFOCXJy+ks8+z2dX8tmVfCaZzzzLZ3/JZx7ndxL5zGIu/eD6+aNIrCESa1aqIEjBOIwEGfB2O3v4ksn5euy93uGSy7Q7upPyn/N+niNCs/rDiYv5TDLzOpHZSOS2luPhb2LBs9Ex3+ydk1O3POEbfw19//G3Z/+o3wvvanN7hohPwpzFov5EIj7Sd8DatSqkBCHutrZYtNEWp/0XZSVW2L7bMxTwdIm/y3sXk2wJROOr0aED8u6Jb1O8q438k0rZR0w8RPxImHqILBYLPxJWcraoDqaUdhVbeQNRH0GaUVrxxQLebo4Sdr+fl6Ue63XYfP4guVb8/tf59NL2q1jm9cP4/a/zmaf5zJN8JpHbWczvPMynY/mdhX998YFmX6vRIau1a1VuSbQoNCDe1Sa3wrjb+qa6YYU5A0GWlVjcbW0Rm7UoSLdnSD4diDsiBdkWF1IB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