Refresh DbContext

I created a project that uses Entity Framework 6. This project is a data layer that is used in several other projects, such as the MVC website and Web API. When the user changes something in the MVC website, it is saved through the data layer in the database. But the Web API project does not detect these changes.

My DbContext is injected into controllers using Ninject. I tried Disposing DbContext in a controller method Dispose

which didn't work. Do I Refresh()

need the database context after it has been injected into the constructor of the controller? Or is there another way to keep the DbContext in sync with the database?

Fwiw, here is the OWIN startup class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        WebApiConfig.Register(config);

        config.DependencyResolver = new NinjectResolver(NinjectWebCommon.CreateKernel());

        app.UseWebApi(config);
    }
}

      

And here is Ninject's code:

public static class NinjectWebCommon
{
    public static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();

        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        GlobalConfiguration.Configuration.DependencyResolver = new WebApiContrib.IoC.Ninject.NinjectResolver(kernel);

        // Register Services:
        kernel.Bind<MyDbContext>().To<MyDbContext>().InRequestScope().WithConstructorArgument("disableOrgFilter", true);

        return kernel;
    }
}

      

+3


source to share





All Articles