logo
Published on

AspNet EF migration

When we develop a web application with AspNet, we do not need to initialize our database with sql command.

We can write code first and use migration command to generate a migration script provided by AspNet EF.

  1. Write code (entity, configuration)
    // entity file
    namespace WebApp.Domain.Entities;
    
    public class FooBar
    {
      public int Id { get; set; }
      public string name { get; set; }
    }
    
    // configuration file
    using WebApp.Domain.Entities;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata.Builders;
    
    namespace WebApp.Infrastructure.Persistence.Configurations
    
    public class FooBarConfiguration : IEntityTypeConfiguration<FooBar>
    {
      public void Configure(EntityTypeBuilder<FooBar> builder)
      {
        builder.HasKey(p => p.Id);
      }
    }
    
  2. Generate migration
    dotnet ef migrations add [CustomizedMigrationName]
    
  3. Apply migration to database
    dotnet ef database update