Found the solution. I needed to create a UnitOfWork class which I created the context within and the repositories. I then used Unity to inject the UnitOfWork class into the controllers with the PerRequestLifetimeManager lifetime manager.
So my UnitOfWork class (which is stored in my DAL project) is:
public class UnitOfWork : IDisposable
{
private static BlogSite.Domain.BlogSiteModelContainer context;
public IBlogRepository blogRepository;
public UnitOfWork()
{
context = new BlogSite.Domain.BlogSiteModelContainer();;
blogRepository = new BlogRepository(context);
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
And I register this simply in my UnityConfig class with the following:
container.RegisterInstance<UnitOfWork>(new UnitOfWork(), new PerRequestLifetimeManager());
In my controllers I then just had a few small changes to make to the constructors so that they would accept the UnitOfWork rather than the repositories.
E.g.
IBlogRepository blogRepo;
public BlogController(UnitOfWork unitOfWork)
{
blogRepo = unitOfWork.blogRepository;
}