How ASP. NET guarantees that the EF operation is unique within the class thread

Source: Internet
Author: User

When it comes to threading, you'll definitely think of a singleton pattern, but there's a problem if multiple users visit the site. There are two ways to ensure that the EF operation class thread is unique (currently only two, and then a good way to add it):

1, HttpContext (the realization principle is also through the data slot CallContext)

The EF operation class is stored in httpcontext.current.items["key" by key-value pair method, which is encapsulated into a method called directly

2, CallContext

public static DbContext Createdbcontext () {

DbContext DbContext = (DbContext) callcontext.getdata ("DbContext");

if (DbContext = = null)

{dbContext = new oaentities ();

Callcontext.setdata ("DbContext", DbContext);

}

return dbContext;

}

How ASP. NET guarantees that the EF operation is unique within the class thread

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.