Entity Framework with NOLOCK, entitynolock

Source: Internet
Author: User

Entity Framework with NOLOCK, entitynolock

Reference page:

Http://www.yuanjiaocheng.net/Entity/jieshao.html

Http://www.yuanjiaocheng.net/entity/tixijiegou.html

Http://www.yuanjiaocheng.net/entity/setenvrionment.html

Http://www.yuanjiaocheng.net/entity/createdatamodel.html

Http://www.yuanjiaocheng.net/entity/modelbrowser.html

In SqlServer, when the same database table is frequently read and written at the same time, there will be a lock problem, that is, before the previous insert, update, and delete transactions are completed, you cannot read data. You must wait until the operation is complete before you can perform the select operation. The purpose is to prevent concurrent operations from reading dirty data. In the SQL statement, if you can tolerate this situation and speed up the query, you can ignore the lock for query:

select * from [User] with(nolock) 

However, if EntityFramework is used in your project, you can use the following code to query nolock: You need to add a reference to the System. Transactions assembly.

//declare the transaction optionsvar transactionOptions = new System.Transactions.TransactionOptions(); //set it to read uncommitedtransactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted; //create the transaction scope, passing our options inusing (var transactionScope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOptions)) { //declare our context    using (var context = new MyEntityConnection()) { //any reads we do here will also read uncomitted data //... //... } //don't forget to complete the transaction scope transactionScope.Complete(); }

Improvement: if there are multiple places in the project that require nolock queries, this code will need to be constantly copied. At this time, we need to consider encapsulation. Obviously, the outer code can be easily extracted, however, the Code segment inside is uncertain. If encapsulated, a code segment needs to be passed in during execution, and the delegation will be used in this case, we can use the delegate to improve it, that is, the logic code used to query the database is passed in by the delegate.

        public static void NoLockInvokeDB(Action action) { var transactionOptions = new System.Transactions.TransactionOptions(); transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted; using (var transactionScope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOptions)) { try { action(); } finally { transactionScope.Complete(); } } }
It is easy to use:
NoLockInvokeDB(() =>
{ using (var db = new TicketDB()) { lst = db.User.ToList(); }});

If you are not familiar with the Statement of Action, which is highly encapsulated by Microsoft (which is essentially a Commission), you can continue to look at the original statement of delegation: (my personal opinion is, microsoft prefers highly encapsulated things and wants programmers to improve programming efficiency. However, beginners will only use it because they do not know how it works, therefore, it becomes a real code migrant workers. Therefore, we should not use it because of our work and study. We should explore its essential implementation more and help our own improvement .)

  public class Helper    {        public void NoLockInvokeDB(EFdelegate d)        {            var transactionOptions = new System.Transactions.TransactionOptions();            transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;            using (var transactionScope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOptions))            {                d();                transactionScope.Complete();            }        }    }    public delegate void EFdelegate();

The call is also very simple

EFdelegate d = new EFdelegate () =>{// code segment written here });

If you are not used to writing this anonymous function, you can write it all.

Protected void Page_Load (object sender, EventArgs e) {EFdelegate d = new EFdelegate (SonFun);} public void SonFun () {// code snippet written here}
References:

Http://stackoverflow.com/questions/926656/entity-framework-with-nolock

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.