开发者

Ninject and Entity Framework

I'm using Ninject with an MVC app, also using EF4.1 Code First. I'm getting a problem when trying to test that I can make a request from two different browser instances.

Basically, if I hit login on both browsers at roughly the same time I get an error telling me that "The context cannot be used while the model is being created."

Now, my first assumption is that I have conflicting instances and am therefore not corre开发者_开发技巧ctly setting the scope on the contexts.

I have a base class context that contains necessary tables, I inherit from this. I then have a factory that is injected, and is responsible for actually creating the context when I request my repository.

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

I need to do this as my connection string is decided at run-time, so I I can't just use the connection string contained in the web.config

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

The idea being that when I need my repository, I inject the repository, it's has it's own injection, creates its context and I can provide some default implementation.

My next concern is that I'm not correctly closing or destroying some instances. I have used someone elses example for setting certain object data in session scope, but most I try and use OnRequestScope.


It sounds like your ContextFactory needs to be bound InRequestScope:

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

The error you are receiving seems to indicate that both requests - one from each browser instance - are trying to use the same exact instance of your EF Context, which of course will not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜