1

I'm starting with the Entity Framework and the repository pattern. I'm confused about the ObjectContext. Is it better to instantiate it each time we need it? I'm using like that:

        private GenericRepository _genericRepository;

        public EmployeeDAO()
        {
            var _context = new NorthwindEntities();
            this._genericRepository = new GenericRepository(_context);
        }

        public Employee FindByID(int employeeID)
        {
            Employee _employee = this._genericRepository.Single<Employee>(x => x.EmployeeID == employeeID );
            return _employee;
        }
Rahma
  • 255
  • 3
  • 21
  • possible duplicate of [Is maintain the transaction with a static LINQ to SQL DataContext in asp.net possible?](http://stackoverflow.com/questions/5324147/is-maintain-the-transaction-with-a-static-linq-to-sql-datacontext-in-asp-net-poss) – Steven Aug 02 '11 at 11:24
  • Read this: http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392 – Ladislav Mrnka Aug 02 '11 at 12:05

1 Answers1

1

Not sure what exactly you mean by Global, but a singleton ObjectContext is not a good idea. The ObjectContext is a Unit of work, and should be pretty much short lived. The exact implementation details may depend on what kind of application you are developing. E.g. for a web application it is quite common to have one ObjectContext instance per web request.

You can also check out similar questions here:

Entity Framework 4 ObjectContext Lifetime

EF - and repository pattern - multiple contexts

Community
  • 1
  • 1
Yakimych
  • 17,612
  • 7
  • 52
  • 69