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/