I have a rather difficult problem. I use FluentValidation.MVC in my ASP.NET MVC 5 project. In it I use the repository template to check if the user's email address is duplicated. The problem is not really in the repository template; this is what the repository uses the Entity Framework context entered into the constructor at runtime:
public class SomeRepository {
The app uses this approach and it works great. SimpleInjector hooks up my EF context, which is designed to use the Per ASP.NET Web request (wraps HttpContext.Items objects).
Dim httpLifecycle = New SimpleInjector.Integration.Web.WebRequestLifestyle(True) container.Register(of IEFContext, EFContext)(httpLifecycle)
No problem with the app here, just a check. When the server receives the mail operation, the error I get is "The operation could not be completed because the DbContext was deleted." It seems like I cannot use any EF-related code in the FluentValidation attribute that uses the EF context for each web request. Nothing special about the validation attribute that does:
public class Val : AbstractValidator<Entity> { public Val() { _repos = Container.GetInstance<ISomeRepos>(); RuleFor(i => i.Email).Must((o, v) => { _repos.HasDistinctEmail(o.ID, v); } } }
The context was supposed to die with the previous request, as it is stored in HttpContext.Items . Any idea what is going on? I know by setting True to WebRequestLifecycle , I invoke the EF context to be deleted on request. I would think that would be desirable.
source share