문제

I have an entity Order with a sub entity OrderDetail. I want to update the Order and add a new OrderDetail. How should i do this?

I've tried setting the state of the Order to modified but i get this error:

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

Update: After i've added the new OrderDetail, i want to update the order type which is part of primary key. I've tried to set the state of OrderDetail to modified but i get a new error:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

도움이 되었습니까?

해결책

Try to put the new OrderDetail into Added state first before you set the Order to Modified:

var newOrderDetail = order.OrderDetails.First();
context.Entry(newOrderDetail).State = EntityState.Added;
context.Entry(order).State = EntityState.Modified;

This might work - or not, depending on the details of your model and scenario...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top