afr0
afr0

Reputation: 888

Repository DbContext and MyDBContext

I'm lost so need some help.

I'm using EF 4.2 and asp.net mvc3. I've Generic Reposiotry that work on DbContext , through IoC I've set up DbContext to be initialized as MyDBContext. It all works fine so far.

I'm using DbContext data member in my Repository class, so it has different APIs than that of MyDbContext. Am I doing it right?

thanks

Upvotes: 0

Views: 620

Answers (1)

Adam Tuliper
Adam Tuliper

Reputation: 30162

If Im not using a unit of work pattern or services, here's how I do it


public class YourController : Controller
{
  private ICustomerRepository  _repository;
  public YourController(ICustomerRepository repository)
  {
     _repository = repository
  }

...
}

public class CustomerRepository : ICustomerRepository
{
   private IContext _context;
  public CustomerRepository(IContext context)
  {
      _context = context;
  }

}

Your object graph is then built by your DI container. ENSURE your context is getting disposed on each request - this varies on configuration depending which DI container you use.

Upvotes: 1

Related Questions