Entity framework with Linq to Entities performance
- by mare
If I have a static method like this
public static string GetTicClassificationTitle(string classI, string classII, string classIII)
{
using (TicDatabaseEntities ticdb = new TicDatabaseEntities())
{
var result = from classes in ticdb.Classifications
where classes.ClassI == classI
where classes.ClassII == classII
where classes.ClassIII == classIII
select classes.Description;
return result.FirstOrDefault();
}
}
and use this method in various places in foreach loops or just plain calling it numerous times, does it create and open new connection every time?
If so, how can I tackle this? Should I cache the results somewhere, like in this case, I would cache the entire Classifications table in Memory Cache? And then do queries vs this cached object?
Or should I make TicDatabaseEntities variable static and initialize it at class level?
Should my class be static if it contains only static methods? Because right now it is not..
Also I've noticed that if I return result.First() instead of FirstOrDefault() and the query does not find a match, it will issue an exception (with FirstOrDefault() there is no exception, it returns null).
Thank you for clarification.