The relationship could not be changed because one or more of the foreign-key properties is non-nullable

前端 未结 20 1078
情书的邮戳
情书的邮戳 2020-11-22 04:44

I am getting this error when I GetById() on an entity and then set the collection of child entities to my new list which comes from the MVC view.

The

20条回答
  •  旧时难觅i
    2020-11-22 05:33

    I've met this problem before several hours and try everything, but in my case the solution was diferent from the listed above.

    If you use already retrieved entity from the database and try to modify it's childrens the error will occure, but if you get fresh copy of the entity from the database there should not be any problems. Do not use this:

     public void CheckUsersCount(CompanyProduct companyProduct) 
     {
         companyProduct.Name = "Test";
     }
    

    Use this:

     public void CheckUsersCount(Guid companyProductId)
     {
          CompanyProduct companyProduct = CompanyProductManager.Get(companyProductId);
          companyProduct.Name = "Test";
     }
    

提交回复
热议问题