简体   繁体   中英

Create and Apply Migration at run time in .Net Core - Entity Framework Core

Is there any possible way to create Migration and apply at run time in dot net core. I have come across context.database.migrate() which creates/update database if migration file is there. for that need to run add-migration from terminal.

is there a way to achieve this adding migration and then running migrate() programmatically from code

Thanks in advance

frist you need add class.

public class InitMigrations { private readonly SDMContext context; public InitMigrations(SDMContext context) { this.context = context; } public void MigrateDatabase() { context.Database.Migrate(); } }

then add startup.cs inject this services

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {

        var initMigrations = serviceScope.ServiceProvider.GetRequiredService<InitMigrations>();
        initMigrations.MigrateDatabase();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM