Repository DbContext and MyDBContext

626 views Asked by At

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

1

There are 1 answers

2
Adam Tuliper On

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.