What causes: "Context cannot be used during model creation."?

I am new to MVC and Entity Framework.

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

I get this error message when I try to make concurrent requests in my repository. For example, one repository is an image caption, so when a page loads, if there is more than one image, this message usually fails:

"Context cannot be used during model creation."

Here is 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 tried to change it to use InTransientScope as well as InRequestScope. No difference.

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

The part where she 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.

Hooray!

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)
+4
source share
1 answer

What is the lifestyle of the underlying DbContext? I assume your repository has a db context dependency, and if your DbContext is registered as single, it will explode. If so, specify your DbContext pr request; creating a DbContext is inexpensive and not intended to be stored as a single instance.

+1
source

Source: https://habr.com/ru/post/1384589/


All Articles