Search code examples
c#linqdatacontext

How should I treat LINQ DB Context objects?


Is it better to create it once and leave it there, re-using it (possibly from a static property) or to create it new on every access to the DB?


Solution

  • Create a new one for each access to the database. Also put it in a using statement:

    using (var db = new MyDatabase())
    {
        // do query
    }