ASP. net mvc 5 Series (6): EF implements embedded and distributed sessions, mvcef

Source: Internet
Author: User

ASP. net mvc 5 Series (6): EF implements embedded and distributed sessions, mvcef

ASP. NET default InProc mode sessions are a waste of memory and there is a data loss problem when the website is restarted. SQLServer mode sessions only support SQL Server and require command line configuration. You can use EntityFramwork to implement M. You can use EntityFramework to implement Custom SessionProvider. This prevents data loss and does not need to be bound to SQL Server. You can also use CodeFirst to automatically generate databases, this eliminates the need for command line configuration.

We can use an embedded database for a single website with a low configuration according to the actual situation, and use a distributed Session server for Server Load balancer sessions to avoid State loss. In addition, we do not need to modify the original code later, only by modifying the web. config allows you to easily add and switch to other databases that provide EntityFramework.

1. Define the object:

Entity code I used the EntityFramework plug-in to reverse generate ASP from the database. NET Session entity code. Refer to other Session providers on github to modify the entity code. The two tables are merged, and the Session access at different Application levels is still supported.

 

2. Custom providers:

There are three main sources for reference:

(1) MongoDB-ASP.NET-Session-State-Store (https://github.com/AdaTheDev/MongoDB-ASP.NET-Session-State-Store)

(2) SQLiteSessionStateStore (https://github.com/micahlmartin/SQLiteSessionStateStore)

(3) System. Web. SessionState. SqlSessionStateStore (ILSpy)

 

3. Configure web. config

You can switch between different data connections by switching the connectionStringName attribute of the Provider.

 

4. test functions:

It's just a simple test of read/write.

 

5. view the result:


Code has been uploaded to gitosc: http://git.oschina.net/myshare/EntityFramework.SessionStateStoreProvider

The component has been uploaded to nuget: https://www.nuget.org/packages/EfSessionStateProvider/

 

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.