开发者

Different C# apps accessing database through Linq to Entities

开发者 https://www.devze.com 2023-04-05 14:20 出处:网络
I have a C# console application which does some processing and then writes to the database. I have it deployed multiple times on a server with different config settings to do slightly different things

I have a C# console application which does some processing and then writes to the database. I have it deployed multiple times on a server with different config settings to do slightly different things. However, they all have to write to the same database (and may need to insert the same data into to the same table if it doesn't already exist) using Linq to Entities.

If I were using threads I could lock the method, or stored procedures I could queue up the writes to avoid clashes, but is there any way to keep these as seperate applications, and prevent them both trying to write the same thing to the database at the same time?

I'm getting an exception every so often when there is a conflict.

Edit:

I'm not necessarily trying to debug why I'm getting the exception, looking more for any suggestions of a 'best practice' way of doing this e.g.开发者_Go百科 Should this be handled at the console app level, the L2E level, or the database level.


Why can't you start a transaction with high isolation level so that the lock is active at the server side?


You may use locks (pessimistic concurrency model) or timestamps (optimistic concurrency model) to deal with concurrency issues.

It is a very wide topic so i would suggest you start by googling for database concurrency.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号