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

前端 未结 20 1067
情书的邮戳
情书的邮戳 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:18

    I found this answer much more helpful for the same error. It seems that EF does not like it when you Remove, it prefers Delete.

    You can delete a collection of records attached to a record like this.

    order.OrderDetails.ToList().ForEach(s => db.Entry(s).State = EntityState.Deleted);
    

    In the example, all of the Detail records attached to an Order have their State set to Delete. (In preparation to Add back updated Details, as part of an Order update)

提交回复
热议问题