0

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?

Sanin
  • 31
  • 2
  • Could you post an example of how you're calling the repository from the worker role? – Sandrino Di Mattia Jun 22 '12 at 05:28
  • Note: UnitOfWork is injected into the ICommand object which is dispatched into by the WorkerRole. Inside the ICommand instances it looks like this: UnitOfWork.LoginRepository.DoSomething(); UnitOfWork.Commit(); – Sanin Jun 22 '12 at 17:42

2 Answers2

0

You are talking about PerResolveLifetimeManager but you are using PerThreadLifetimeManager (which is said to be buggy anyways) in your code. May that be the cause of your problems?

Sebastian Weber
  • 6,566
  • 2
  • 27
  • 46
0

Ok, I refactored the solution to do much more explicit Resolve calls instead of .ctor injection and then after all that I realized the main issue was around trying to update an object while iterating over an IQueryable that contained it.

Much ado about nothing, seems to me.

Thanks for the help.

Sanin
  • 31
  • 2
  • Ok, I had hopes when I saw this question title..but then you responded with this answer, which doesn't help me. I've posted my question http://stackoverflow.com/questions/17532233/the-underlying-provider-failed-on-open-error-when-using-ef5-code-first-with-un – Thiago Silva Jul 08 '13 at 17:08