Skip to content

aspnet-modules/aspnet-module-migrator

Repository files navigation

AspNet.Module.Migrator

Database migration package for applying EF Core migrations and running seed data.

Usage Steps

1. Create a migration DbContext

public class MigrationDbContext : AppDbContext
{
}

2. Add Microsoft.EntityFrameworkCore.Design

This enables dotnet ef migrations add and other EF Core CLI commands.

<ItemGroup>
    <PackageReference Include="AspNet.Module.Migrator" Version="3.x.x" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
</ItemGroup>

3. Create a design-time DbContext factory

public class MigrationDbContextFactory : BaseDesignTimeDbContextFactory<MigrationDbContext>
{
}

4. Generate migrations from CLI

dotnet ef migrations add Init
dotnet ef migrations remove

5. Configure the migrator in Program

var migrator = new AspNetMigrator<MigrationDbContext>(new AspNetMigratorOptions(args)
{
    Configuration = c =>
    {
#if DEBUG
        c.AddUserSecrets(Assembly.GetEntryAssembly());
#endif
    }
});

migrator.Seeds.Add<Seed1>();
migrator.Seeds.Add<Seed2>();

await migrator.RunAsync();

6. Add the migrator to a Docker image

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
RUN dotnet restore src/Some.Migrator/*.csproj
WORKDIR /app/src/Some.Migrator
RUN dotnet publish -o out -c Release --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
COPY --from=build /app/src/Some.Migrator/out ./migrate/

Seed Registration

internal class SomeSeed : ISeedDatabaseExecutor<MigrationDbContext>
{
    public int Order => 1;
    public string Name => nameof(SomeSeed);
    public bool Enabled => true;

    public Task Execute(MigrationDbContext dbContext, CancellationToken ct)
    {
        dbContext.SomeEntity.Add(...);
        return Task.CompletedTask;
    }
}
var migrator = new AspNetMigrator<MigrationDbContext>(new ConsoleMigratorConfig(...));
migrator.Seeds.Add<SomeSeed>();
await migrator.RunAsync();

Source Code

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors