Entity framework with Linq to Entities performance

Posted by mare on Stack Overflow See other posts from Stack Overflow or by mare
Published on 2010-05-14T18:01:08Z Indexed on 2010/05/14 18:04 UTC
Read the original article Hit count: 202

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?

  1. 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?
  2. Or should I make TicDatabaseEntities variable static and initialize it at class level?
  3. Should my class be static if it contains only static methods? Because right now it is not..
  4. 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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework