Answer the question
In order to leave comments, you need to log in
ASP.NET + Problems with Ninject. Updated data is not updated in DBContext. Where is the mistake?
Good day.
Faced a specific problem in my opinion.
Description of the problem:
The site runs on ASP.NET MVC, using Ninject, requests to the database are made through asynchronous requests.
There is Repository with IRepository interface, UnitOfWork with IUnitOfWork, IDatabaseFactory with DataBaseFactory and services that work with repositories.
All this is connected through Ninject:
Bind<IUnitOfWork>().To<UnitOfWork>().InSingletonScope();
Bind<IDatabaseFactory>().To<DatabaseFactory>().InSingletonScope();
this.Bind(x => x
.From(typeof (UserRepository).Assembly)
.SelectAllClasses().InNamespaceOf(typeof (UserRepository))
.EndingWith("Repository")
.BindAllInterfaces()
.Configure(b => b.InSingletonScope()));
this.Bind(x => x
.From(typeof (UserService).Assembly)
.SelectAllClasses().InNamespaceOf(typeof (UserService))
.EndingWith("Service")
.BindAllInterfaces()
.Configure(b => b.InSingletonScope()));
Answer the question
In order to leave comments, you need to log in
Found a solution to this problem uninstalled Ninject and installed AutoFac - everything works fine.
I immediately post a piece on setting up AutoFac, suddenly someone will encounter the same problem:
1. Install the nuget package AutoFac and AutoFac API
2. Add the setting function to Global.asax
private void RegisterIOC()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterApiControllers(typeof(MvcApplication).Assembly);
Core.AutofacConfiguration.Init(builder);
builder.RegisterFilterProvider();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Autofac.Integration.WebApi.AutofacWebApiDependencyResolver(container);
}
public static class AutofacConfiguration
{
public static void Init(ContainerBuilder builder)
{
//Пример настройки
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
}
}
RegisterIOC();
"but when fetching changed data, the changed field from the repository returns the old value that was before the change." - different database contexts are probably created, one when the data is loaded, the other when the data changes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question