Storing a global object in ASP.NET MVC c# entity-framework-6


I'm currently building a rather large ASP.NET MVC application with user logins. Some methods/functions are restricted to users of a certain type/role (not the regular ASP.NET Role, though) and when running these functions, I have to check in the database if the current, logged in user has access to perform the given task.

As you probably can imaging, this leads to quite a few calls to the database, to get the person. So, currently, for every page load and every restricted method, I'm making this call:

var person = ctx.People
             .Include(x => x.City)
             .Include(x => x.Country)
             .FirstOrDefault(x => x.ID == personId);

Since I need all of these informations for globally displayed data.

Is there any way to simply make this call once (i.e. when a user logs in) and then store that object somewhere for later use? Obviously, a Session variable could be used, but since the person object contains some sensitive data, I'm not sure if that would be a good idea ;-)

Any help is greatly appreciated!

Thanks in advance.

12/2/2014 11:54:07 AM

Accepted Answer

Depending on the scope that you wish to have, you can use:

  • Session: for the current user, in the current navigation session (no other user can see it);
  • Cache: for all users, with a fixed expiration;
  • Application: for all users, with no expiration.

For cache and application, the key might be composed by some user property, such as id or login. These guys ( have implemented a second level cache for Entity Framework, might be worth checking it out.

12/2/2014 12:00:56 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow