Thursday 15 April 2010

.net - Avoiding concurrency issues with DbContext and Entity Framework -


I am creating ASP Web API project, which is using SQL Server for storage, and the unit To connect to the framework.

In many tables in storage there are unique obstacles to implement the specificity of values ​​in them. Before adding values ​​to these tables, to ensure that my prices are not present, my application check

This application has been created so that a DBCNode is present on every request. It has happened to me, however, two can try to include the same value in the request table. Request A checks the existence of the value of the table, as the request b does. Both have to find out that the value is required to be added, and add it to DBST. When called SaveChanges, a request will be successful, and failure will fail due to one.

How can these types of issues be resolved by the entity framework? I am planning to create stored procedures to retrieve or create prices, which I think should solve it (stored procedures that guarantee nuclear), but I completely solve it with unit framework To be installed in a way I think I'm dabantex. Save Changel () is called, but clearly stored procedure solution will mean that SaveChanges () will be done before executing the database.

This problem is not specific to the unit framework (although the EF certainly limits other boundaries around unique obstacles Too) This is common with any similar server client model. Since the specification will be applied to the database server at the time of the request, if no one tries to condemn, then unless you try to insert it, no idiots will be there.

He said, checking in advance for uniqueness should be a very cheap operation (on SQL Server, a unique barrier will also give you an index on that column), depending on whether How often do you expect to try the duplicate entry of the application, in your case on a large scale, but if it is too rare to start the efforts to put a duplicate, Inserting images can escape the round-trip and so be able to handle the exception.

There is no need to handle the exception is very complex, especially if there is no way to modify the data for the end user and try again. In this case, as you said, you are stopping many entries in a transaction, you may have to check exceptions (or internal exceptions) for specific obstruction names, so that you know which unit object is a duplicate . (Again, EF does not really support the handling of unique obstacle exceptions, so this is probably the part that is causing you the most pain yet.)

Note: < / Strong> do not recommend putting in the stored procedure only, who try to get duplicate entries concurrently for rare occasions. You're better off making sure your app handles this (and other SQL exceptions, many of which can be), as much as possible. However, if you feel that there is really no other way in your case, then a sproc can be the easiest way to do now. Just keep in mind that this is probably the most cautious code to solve a very rare case.

No comments:

Post a Comment