views:

42

answers:

1

Hi, I have EntityTypes generated from a database using Entity Framework 4. I would like to use Cache to store some of these EntityTypes for performance reasons. Is it safe to do the following provided that the object will be used for read-only actions:

context.Students.MergeOption = MergeOption.NoTracking;
var students = context.Students.Where(s => s.Name == "Adam").ToList();
Cache["students"] = students;

Thanks.

+3  A: 

One way is to use the EF cache provider. Another is to use your web server's cache. But make sure you are not prematurely optimizing, or optimizing the wrong thing. Doing so will make your life miserable. It is generally better to optimize a web site with front end caching.

Craig Stuntz
Please consider my question again. I edited it. Thanks
Nazaf
Your edit doesn't change my answer. "Optimizing" before profiling is usually wrong.
Craig Stuntz
I know that EF cache provider is a solution. However, my code might be simpler to use. So my code is not safe. Is that what you mean?
Nazaf