4
votes

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)
1
Just added it to my post. Thanks!kodikas
Where is the failing code called? Is it happening only when you restart the application or all the time? How many concurrent request is running when this happens?Ladislav Mrnka
I have a controller for a page. It builds the page, calling the GetImage(string id) function. The GetImage function then accesses the CurrentCaptionRepository and tries to retrieve the caption. The home page has two images and usually (but not always) causes the problem. It happens anytime I try to build a page that has more than one picture (and thus two requests for the image).kodikas

1 Answers

1
votes

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.