Entity Framework deadlock problem

那年仲夏 提交于 2020-01-15 08:43:25

问题


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?


回答1:


Per your comment:

TableB didn't have primaryKey set in the database where issue was reproduced



来源:https://stackoverflow.com/questions/3520639/entity-framework-deadlock-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!