I have an Azure worker role with a UnitOfWork class that looks something like this:
public class UnitOfWork : IUnitOfWork
{
public MyData Db { get; private set; }
public ILoginRepository LoginRepository { get; private set; }
public ISubscriptionRepository SubscriptionRepository { get; private set; }
public UnitOfWork(MyData db,
ILoginRepository loginRepository,
ISubscriptionRepository subscriptionRepository)
{
}
}
The repositories accept a reference to the DbContext as well:
public class LoginRepository : Repository<Login>, ILoginRepository
{
public LoginRepository(MyData db) : base(db) { }
}
I would think this is pretty straight-forward.
Now, I'd like to configure my code-first DbContext in Unity so that each time a UnitOfWork is resolved a new DbContext is created and all subsequently resolved repositories get it too.
I'd think that PerResolveLifetimeManager would do the trick:
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<MyData, MyData>(new PerResolveLifetimeManager());
But it does not. I get all these weird SQL-related errors, such as:
"New transaction is not allowed because there are other threads running in the session."
What gives?