0 0
Read Time:37 Second

Application DB Context :

  public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
        {
   
        }

	public DbSet<ModelofTheProgram> ModelofThePrograms { get; set; }
}

Migration Created Using the below command:

add-migration MigrationFIleName

Migration File created with the extra snapshot file. you can check in the migration folder for more details.

Once the migration file is created then only auto migration code may run.

           try
            {
                if (_db.Database.GetPendingMigrations().Count() > 0)
                {
                    _db.Database.Migrate();
                }
    
       
            }catch(Exception ex)
            {

            }

Try the above code when the program starts.

Register and run the program from the program.cs

SeedDataBase(); in the pipeline of program.cs

Final code to start migration:

void SeedDataBase()
{
    using ( var scope= app.Services.CreateScope())
    {
        var dbInitializer = scope.ServiceProvider.GetRequiredService<IDbInitializer>();
        dbInitializer.Initialize();
    }
}

Thank you

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “Run Auto Migration in Asp.Net Core 6 MVC

Leave a Reply

Your email address will not be published. Required fields are marked *