【发布时间】:2012-04-04 07:26:45
【问题描述】:
当我从实体框架查询时,我总是在分离状态下查询,以便检索到的记录可以存储在缓存中以供后续请求使用。
现在我有一个用户可以编辑的表单,其中包含一个父记录,然后是两个父记录列表。
当数据发布到服务器时,我使用我的视图模型并将它们映射到使用 AutoMapper 的实体框架对象中。数据看起来不错; AutoMapper 正在正确映射数据。
当我附加对象以便更新它时,会引发异常: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。
public static void UpdateOrder(ShippingOrder shippingOrder) {
using (OrderEntity orderContext = new OrderEntity()) {
//Exception happens here
orderContext.ShippingOrders.Attach(shippingOrder);
//Update the order itself; mark the order has being modified so the EF will update it.
orderContext.ObjectStateManager.ChangeObjectState(shippingOrder, System.Data.EntityState.Modified);
//Perform the update.
orderContext.SaveChanges();
}
}
EntityFramework (EF) 似乎认为我的键没有对齐,但我不确定什么是不正确的。外键属性确实有正确的值,所以我不确定它在检查什么。有人有什么想法吗?
【问题讨论】:
标签: asp.net asp.net-mvc-3 entity-framework-4