kodikas
kodikas

Reputation: 397

What is causing: "The context cannot be used while the model is being created."?

I'm new to MVC and Entity Framework.

I have a simple Azure MVC 3 web application. I am using Entity Framework and Ninject, and my data is stored in an SQL database.

I am getting this error message whenever I try to make concurrent requests to my repository. For example, one repository is image captions, so when the page is loading, if there is more than one picture it usually fails with this message:

"The context cannot be used while the model is being created."

Here's my connection string:

<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TheDBName; Integrated Security=SSPI; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>

For Ninject, when I bind the interface I have tried changing it to use InTransientScope and also InRequestScope. No difference.

this.ninjectKernel.Bind<ICaptionsRepository>().To<EFCaptionRepository>().InTransientScope();

The part where it fails is here:

Caption foundCaption = currentCaptionRepository.Captions.FirstOrDefault(a => a.ID == pictureID);

Please let me know if you need any other information to help find the problem.

Cheers!

EDIT: Here's the stack trace

   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at MVCProject.WebUI.Controllers.ImageController.GetImageByIDWithSize(String id) in C:\MVCProject\MVCProject\MVCProject.WebUI\Controllers\ImageController.cs:line 131
   at MVCProject.WebUI.Controllers.ImageController.GetImage(String id) in C:\MVCProject\MVCProject\MVCProject.WebUI\Controllers\ImageController.cs:line 215
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.c__DisplayClass15.b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

Upvotes: 4

Views: 3892

Answers (1)

Marius
Marius

Reputation: 9674

Whats the lifestyle of the underlying DbContext? I am guessing that your repository has a dependency on the db context and if your DbContext is registered as a singleton this will blow up. If this is the case, scope your DbContext pr request, creating a DbContext is inexpensive and it is not intended to be kept as a single instance.

Upvotes: 1

Related Questions