---
title: "How to run Migration automatically in program.cs in .Net Core 6."  
description: "How to run Migration automatically in program.cs in .Net Core 6."  
author: "Utpal Vishwas"  
published: 2023-11-07  
updated: 2023-11-08  
canonical: https://www.mindstick.com/forum/160454/how-to-run-migration-automatically-in-program-cs-in-dot-net-core-6  
category: ".net core"  
tags: ["asp.net core", ".net core", ".net core 6"]  
reading_time: 2 minutes  

---

# How to run Migration automatically in program.cs in .Net Core 6.

How to run [Migration](https://www.mindstick.com/articles/23202/magento-1-to-magento-2-migration-the-ultimate-guide-updated-2018) automatically in the program.cs in .Net Core 6.

## Replies

### Reply by Aryan Kumar

In .NET Core 6, you can run database migrations automatically when your application starts by adding code in the **Program.cs** file. This can be useful to ensure that the database is up-to-date before the application starts. Here's how you can achieve this:

**Install Entity Framework Core and Migrations**: Make sure you have Entity Framework Core and the necessary packages for your database provider installed in your project. If not, you can add the packages using the following commands:

For SQL Server, for example:

```plaintext
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
```

For SQLite:

```plaintext
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
```

Be sure to install the package that corresponds to your database provider.

**Add Database Context**: Create a database context class that inherits from **DbContext**. This class represents the database and includes the **DbSet** properties for your models. For example:

```plaintext
using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
    public DbSet<YourEntity> YourEntities { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
```

**Configure and Run Migrations in Program.cs**: In your **Program.cs** file, configure the database context and apply migrations as follows:

```plaintext
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting
```

In the code above:

- We create the host and a service scope using **CreateScope()**.
- Inside the scope, we retrieve the database context using dependency injection.
- We apply any pending migrations and update the database using **dbContext.Database.Migrate()**.

**Replace YourEntity and Connection String**: Replace **YourEntity** with the actual entity class used in your application and configure the database connection string in the **CreateHostBuilder** method.

Now, when you run your .NET Core 6 application, it will automatically apply pending database migrations before starting the application. This ensures that your database is up-to-date with the latest schema changes.


---

Original Source: https://www.mindstick.com/forum/160454/how-to-run-migration-automatically-in-program-cs-in-dot-net-core-6

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
