2

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your edit doesn't change my answer. "Optimizing" before profiling is usually wrong.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.