Ninject and Entity Framework

I am using Ninject with an MVC application, also using EF4.1 Code First. I am having a problem trying to verify that I can make a request from two different browser instances.

Basically, if I hit the login in both browsers at about the same time, I get an error message indicating that "the context cannot be used during model creation."

Now my first assumption is that I have conflicting instances, and therefore I am setting the scope of contexts incorrectly.

I have a base class context that contains the necessary tables, I inherit this. Then I create a factory that is responsible for actually creating the context when querying my repository.

public class ContextFactory
{
     TContext Create<TContext>( ) 
}

I need to do this, since my connection string is determined at runtime, so I cannot just use the connection string contained in the web.config file

public class Repository : BaseRepository<MyObject>
{
   public Repository(IContextFactory factory) : base(factory) 
   {
   }
}

The idea is that when I need my repository, I insert the repository, it has its own injection, its context is created, and I can provide some default implementation.

My next concern is that I am not properly closing or destroying some instances. I used the elses example to set certain object data in the session area, but most of them I try to use OnRequestScope.

+3
source share
1 answer

It looks like yours ContextFactoryshould be related InRequestScope:

Bind<IContextFactory>().To<ContextFactory>().InRequestScope();

, , , - - EF Context, , , .

+4

All Articles