实体框架死锁问题
我在使用 .NET 4 的新实体框架时遇到了一个奇怪的问题
我有一个 SQL Server 2005 (EXPRESS) 数据库,我的服务使用实体框架将数据写入两个表。假设表是 TableA 和 TableB。 TableB 具有 TableA 的外键。在我的程序中,有几个并行写入数据的线程。每个线程都有自己的ObjectContext。程序创建 TableA 对象并将其放入对象上下文中。然后创建 TableB 的对象并将其放入对象上下文中。对于几个 TableA 项目重复此操作。
最后,当调用 ObjectContext.Save 方法时,我在日志中看到死锁错误:
System.Data.SqlClient.SqlException: Transaction (Process ID 56) was deadlocked on lock resources with another process and has been selected 作为死锁受害者。重新运行事务。
似乎它在某种程度上取决于数据库选项,因为它仅在特定数据库上重现。一个新的数据库没有重现该问题。当实体框架执行批量插入时,哪些数据库\表设置会影响死锁?或者可能是其他地方的问题?
I'm having a strange problem with new Entity Framework for .NET 4
I have a SQL Server 2005 (EXPRESS) database and my service writes data to two tables using entity framework. Let's say tables are TableA and TableB. TableB has foreign key to TableA. In my program there are several threads that writes data in parallel. Each thread has its own ObjectContext. The program creats TableA object and puts it in the object context. Then objects for TableB are created and put to object context. This repeats for several TableA items.
Finally when ObjectContext.Save method is called I see a deadlock error in the log:
System.Data.SqlClient.SqlException: Transaction (Process ID 56) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
It seems that it somehow depends on database options as it is reproduces on particular database only. One new database the issue is not reproduced. What database\table settings can influence on deadlock while Entity Framework performs batch insert? Or may be the problem somewhere else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的评论:
Per your comment: