Implementing if-not-exists-insert using Entity Framework without race conditions

前端 未结 2 1691
心在旅途
心在旅途 2021-02-01 07:51

Using LINQ-to-Entities 4.0, is there a correct pattern or construct for safely implementing \"if not exists then insert\"?

For example, I currently have a table that tra

2条回答
  •  一生所求
    2021-02-01 08:42

    You could try to wrap it in a transaction combined with the 'famous' try/catch pattern:

    using (var scope = new TransactionScope())
    try
    {
    //...do your thing...
    scope.Complete();
    }
    catch (UpdateException ex)
    {
    // here the second request ends up...
    }
    

提交回复
热议问题