Home > Enterprise >  Migration in .Net 6.0 project to create database
Migration in .Net 6.0 project to create database

Time:11-18

I add migration to my .Net 6.0 project and when I run command "update-database", the database is created but when I don't run this command and run my project, it doesn't create the database

CodePudding user response:

and run my project, it doesn't create the database

You should run it programmatically, something like this:

using (var context = new MyContext(options))
{
  await context.Database.MigrateAsync();
}

If there was no any pending migrations, this code does kinda nothing. For instance, if you had the only migration for creation db and some initial tables, db will not be recreated. List of applied migrations you can see in special table called _MigrationsHistory.

You can run also this one (but no migrations are applied):

context.Database.EnsureCreated();

That's all. Hope it will help.

CodePudding user response:

Run Database.Migrate in your application database context, to run command "update-database" programmatically.

public class MyContext : IdentityDbContext {
    public MyContext() {
        Database.Migrate();
    }
}
  • Related