Dot net/EF core auto update migrations in database

Dot net core Entity framework migrations auto update into database.

We can update migrations in database automatically, or we can update manually using 
Update-database command in package manger console as mentioned in blow picture.





Automation process for updating migrations in database:

we can update migrations automatically by using  Database.Migrate()



Add below code in to your project as per above image.

public static void Initialize(IServiceProvider service)

        {

            using (var serviceScope = service.CreateScope())
            {
                var scopeServiceProvider = serviceScope.ServiceProvider;
                var db = scopeServiceProvider.GetService<WebAPIDataContext>();
                db.Database.Migrate();

            }

        }



public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            Initialize(app.ApplicationServices);
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }


The above code will automatically create database after initializing solution.



Dot net core | EF Core | Dot net core 2.2 | Entity Framework | Auto migration | migrate database| EF core migrations | 







No comments