Code and prod logo

Code and Prod Complet Projects

Follow me

EL JIHAOUI
QUICK ACCESS

EF migrations add database update .NET7

ef migrations add database update .NET7

Table of Contents

EF migrations

Migrations is a way to keep your database schema and EF Core model in sync by preserving your data.

EF migrations are a set of commands which you can execute in NuGet Package Manager Console (PMC) or in dotnet Command Line Interface (CLI).

In Entity Framework Core, To generate the migration scripts for creating the database of our project, we will need two commands from the Entity Framework Core package: dotnet ef migrations Add (Add-Migration) and dotnet ef database update ( Database-update).

These two commands can be executed according to the instructions given in the table below:

under Nuget Package Manager integrated with Visual StudioUsing the Visual Studio Code Terminal
Add-Migration dotnet ef migrations Add
Database-Update dotnet ef database update

EF migrations add

We already have the two objects we need, the Model “Book” and the DbContext “AppDbContext” define as follows:

				
					   public class Book
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; } = string.Empty;
        public string? Description { get; set; }
    }
				
			
				
					    public class AppDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseSqlServer(@"Server=.\SQLEXPRESS; Database=EFCoreDb;Trusted_Connection=True;Encrypt=False;");
        }
        public DbSet<Book>? Books { get; set; }
    }
				
			

Then we will execute the following command, in the Terminal of Visual Studio Code Dotnet ef migrations add initDatabaseScript to generate the migration script:

				
					dotnet ef migrations add initDatabaseScript
				
			

The generated script is as follows:

				
					 public partial class InitDatabaseScript : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Books",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
          
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Books", x => x.Id);
                });
        }
          protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Books");
        }
    }
				
			

EF database update

To run this script in  “EFCoreDb” database, we will run the following command:

				
					dotnet ef database update
				
			

Following this command, the “EFCoreDb” database will be created at the SQL SERVER, and the “Books” table will also be created respecting the characteristics mentioned in the “Book” class/Model

the following pictures show the tables created in the SQL SERVER database “EFCoreDb

database-efcoredb
database-efcoredb-table-books
database-efcoredb-migrations

Course Video

if you like it share it please

Facebook
LinkedIn
Twitter
Related Posts
Data Seeding in migration using EF Core
Entity Framework Core
EL JIHAOUI

Data seeding

Data seeding is a process where you populate a database with an initial set of data
There are

Read More »

MORE RESSOURCES