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("Person_Firm_PersonResponsibility.Firm") .Include("Person_Firm_PersonResponsibility.PersonResponsibility") .Include("Person_Firm_PersonOption.PersonOption") .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.
Depending on the scope that you wish to have, you can use:
For cache and application, the key might be composed by some user property, such as id or login. These guys (https://efcache.codeplex.com/) have implemented a second level cache for Entity Framework, might be worth checking it out.