Manage concurrency with entity framework RRS feed

  • Question

  • Something is still wrong!

    I have a n-layer application with entity framework... I create a transaction for each browser:

    this._transaction = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 5, 0));

    I am trying to insert at the same time (with two different browser) two customers in a table... 

    The ID of the customer is like an identity (first digit identify the type of customer + year + incremental number).

    Before I insert the customer i check if that id is already exist:

    bool existCustomerCode = this._customerACL.GetCustomers(customer.Type).Exists(c => c.CustomerCode == customer.CustomerCode);

    if (existCustomerCode) { throw new DuplicateCustomerCodeException(); }

    The method GetCustomer do a query on Entity Framework:

    from c in base._dataContext.Customers
        where c.IdTypeCustomer == customerType
        select c

    The wishes result is something like this:

    - First browser insert the customer;

    - Second browser throw new DuplicateCustomerCodeException();

    I have this result instead (with conccurencyMode = NONE):

    - First browser, insert the customer;

    - The second one, stop on instruction that query entity framework and then go in timeout... but after i do an AcceptAllChanges in first transaction.

    OR (with conccurencyMode = FIXED):

    - First browser, insert the customer;

    - The second one, throw a OptimisticConcurrencyException .

    How can i have the wished result?

    Thanx on advanced

    • Moved by CoolDadTx Monday, August 27, 2012 2:08 PM EF related (From:Visual C# General)
    Thursday, August 23, 2012 10:20 PM