Domanda

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.

È stato utile?

Soluzione

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...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top