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

前端 未结 20 1336
情书的邮戳
情书的邮戳 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条回答
  •  余生分开走
    2020-11-22 05:22

    If you are using Auto mapper and facing the the issue following is the good solution, it work for me

    https://www.codeproject.com/Articles/576393/Solutionplusto-aplus-Theplusoperationplusfailed

    Since the problem is that we're mapping null navigation properties, and we actually don't need them to be updated on the Entity since they didn't changed on the Contract, we need to ignore them on the mapping definition:

    ForMember(dest => dest.RefundType, opt => opt.Ignore())
    

    So my code ended up like this:

    Mapper.CreateMap
    ForMember(dest => dest.NavigationProperty1, opt => opt.Ignore())
    ForMember(dest => dest.NavigationProperty2, opt => opt.Ignore())
    .IgnoreAllNonExisting();
    

提交回复
热议问题