-2

Does this static class reuse Entity Framework connection and is it thread safe?

public static class TeamService
{
    public static Team GetTeam(string id)
    {
        using (var Db = new ApplicationDbContext())
        {
            //TODO:
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Saleh Bagheri
  • 424
  • 4
  • 19

1 Answers1

1

By default, the context manages connections to the database. The context opens and closes connections as needed. For example, the context opens a connection to execute a query, and then closes the connection when all the result sets have been processed.

-- https://msdn.microsoft.com/en-us/data/jj729737

A separate instance of context for each thread is thread-safe. As long as each thread of execution has its own instance of EF context you will be fine.

-- https://stackoverflow.com/a/4455827

Community
  • 1
  • 1
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • 1
    Note that there is a distinct difference between a *connection* and a *context*. Having a context per call doesn't necessarily mean there is a *connection* per call. – Rob Jan 13 '16 at 23:00
  • Thanks for point out my mistake. – Xiaoy312 Jan 13 '16 at 23:12