c# - parallel.invoke transaction scope entity framework -
In my EF4 program, I have an applicant and application table. Many examples of the program run from time to time to make applications for applicants based on some business logic. In the application form, I can not have more than one submission / record for an applicant.
So here's the piece of code that checks whether there is a submitted application and it is included. It is run in a foreach loop for the list of applicants. Above the code, prevents duplicate records from being inserted separately from the fact that it throws UpdateException while running several instances of the program. If I swallow that exception and move on then everything is fine. However, I tried to test / run the code in parallel but it includes duplicate records in the database. In the upper part of the code, this update throws exception and multiple applications for an applicant The row is inserted. Note that the table contains only one primary key of rental and no other unique obstacles. My question is: Why TransactionScope is in Parallel.Invoke Running duplicate rows included But does not I have many examples of program fire off? What a sound approach it will have to achieve UPDATE: SqlUnitOfWork has ctor MyEntities are generated by ctor ef - Thanks. This is a classic race condition. Both transactions check to see if there exists a pre-existing You need to synchronize your check, either by locking in the application or by taking a SQL server
Parallel.Invoke ((=) CreateApplications ("Parallel Example 1"), () = & gt; CreateApplications ("Parallel Example 2")); Private Zero CreateApplications (String Dummy Instance Name) {var unitOfWork = New SqlUnitOfWork (); Var Applicants = UnitOffork Applications.findell (). Take (100) .Olist (); Var Mask = New Provider Facade (Unitofwork, New Log 4 Netlogger (Dummy Instance-name)); Foreach (applicant applicant applicant) (Mask Application Provider. Approval (Applicant. APPLICANT ID);}}
public SqlUnitOfWork () {_context = New MyEntities ();}
public constant string ConnectionString = "name = units"; Public Construction String ContainerName = "Unit"; Public TPIEntities (): Base (Connection String, Container Name) {this.ContextOptions.LazyLoad IngEnabled = true;}
application . Both of them find that there is no one, then try to include both of them.
UPDLOCK, HOLDLOCK by checking whether
applications is present.
Comments
Post a Comment