实体框架/EF4:在事务范围中多次插入相关实体
我有类似的问题。
我想在同一个事务范围中进行两次插入。这些对象是相关的并且它们之间具有 FK 关系,但由于多种原因,我不想通过导航属性连接它们,而只想通过 ID 连接它们。
这是我想要完成的任务的简化:
Order o = new Order();
OrderDetails d = new OrderDetails();
new Repository().SaveNew(o, d);
class Repository{
void SaveNew(Order o, OrderDetails d){
using (TransactionScope transaction = new TransactionScope())
{
_context.Connection.Open();
// order
_context.Orders.ApplyChanges(o);
_context.SaveChanges();
// details
d.OrderID = o.ID;
_context.OrderDetails.ApplyChanges(d);
_context.SaveChanges(); <--- UpdateException
_context.Connection.Close();
transaction.Complete();
}
}
}
问题是我收到 UpdateException,因为 FK 评估失败。我尝试删除 FK 关系并运行完全相同的代码,效果很好,并且两个对象都设置了正确的属性。那么为什么这种方法会失败呢?应该如何做呢?同样,我不想通过实体的导航属性附加实体。
谢谢你!
I have a similar problem.
I want to make two inserts in the same transactionscope. The objects are related and have a FK relationship between them, but for several reasons I do not want to connect them via the navigation property, but only by ID.
This is a simplification of what I what I want to accomplish:
Order o = new Order();
OrderDetails d = new OrderDetails();
new Repository().SaveNew(o, d);
class Repository{
void SaveNew(Order o, OrderDetails d){
using (TransactionScope transaction = new TransactionScope())
{
_context.Connection.Open();
// order
_context.Orders.ApplyChanges(o);
_context.SaveChanges();
// details
d.OrderID = o.ID;
_context.OrderDetails.ApplyChanges(d);
_context.SaveChanges(); <--- UpdateException
_context.Connection.Close();
transaction.Complete();
}
}
}
The problem is that I get an UpdateException because the FK evaluation fails. I tried to remove the FK relationship and running the exact same piece of code, and it worked fine, and both objects had the right properties set. So why does this approach fail? And how should this instead be done? Again, I do not want to attach the entites via their navigation properties.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有运行开发环境来测试这一点,但我认为正在发生的是:
假设 id 是在数据库中生成的。当您保存订单时,您不知道 ID。
然后订单详细信息的订单 ID 设置为订单的 ID,但订单并未从数据库重新加载。我怀疑该值为 0。
当您尝试使用 FK 为 0 保存订单详细信息时,会出现错误。
同时保存两者,以便 EF 为您完成工作,或者重新加载订单。
I do not have development environment running to test this, but what I think is happening is:
Assuming that the id is generated in the database. At the point when you save the order you do not know the ID.
Then the order ID of the order detail is set to the ID of the order, but the order was not reloaded from the database. I suspect that the value is 0.
When you try to save the order detail with FK of 0, you get an error.
Either save both at the same time so that EF does the work for you, or reload the order.
我会将 FK 关系保留在数据库中,但从 SSDL 中删除 AssociationSet 和 Association。设计器不会让你这样做,你必须手动编辑 XML。
顺便说一句,我正在使用 EF 4。
然后在 SaveNew 方法中使用 AddObject 和 SaveChanges 添加第一个(父)对象。在子项上设置外键属性并使用 AddObject 和 SaveChanges 添加它。
I would leave the FK relationship in the database, but delete the AssociationSet and Association from the SSDL. The designer won't let you do this, you have to edit the XML manually.
I am using EF 4 btw.
Then use AddObject and SaveChanges in your SaveNew method to add the first (parent) object. Set the foreign key Property on the child and add it with AddObject and SaveChanges.