Skip to content

Latest commit

 

History

History
114 lines (99 loc) · 3.07 KB

asp-net_hints.md

File metadata and controls

114 lines (99 loc) · 3.07 KB

Some hints and howto for asp.net

Entity framework

Work with relations

  • Example
    // the db Context 
    public class ApplicationDbContext : DbContext, IApplicationDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        public DbSet<RadariaData> Dataset { get; set; }
    }

    public class Match
    {
        // don't forget to add a Id
        public int Id { get; set; }

        public string Name { get; set; }
        public IEnumerable<GameSituation> Situations { get; set; }

        // relation to containing Matches
        public int DatasetId { get; set; }
        public RadariaData Dataset { get; set; }
    }

    public class RadariaData
    {
        public int Id { get; set; }
        public IEnumerable<Match> Matches { get; set; }
    }
}

Initialize DB Code first

  • Create init method and call it at the end of the ConfigureServices (Startup.cs) - InitializeDatabase(services);
private static void InitializeDatabase(IServiceCollection services)
{
    // avoid init when called from migration creation
    if ("ef" == Assembly.GetEntryAssembly().GetName().Name.ToLower(CultureInfo.InvariantCulture))
    {
        return;
    }

    static void Migrate(DbContext context)
    {
        // f.e. when using in unit testenvironment
        if (context.Database.ProviderName != "Microsoft.EntityFrameworkCore.InMemory")
        {
            context.Database.Migrate();
        }
    }

    using var serviceProvider = services.BuildServiceProvider();
    var appCtx = serviceProvider.GetRequiredService<ApplicationDbContext>();
    Migrate(appCtx);
}
  • Create migration method (call it at end of end of configure (Startup.cs) MigrateDatabase(app);)
private static void MigrateDatabase(IApplicationBuilder app)
{
    if ("ef" == Assembly.GetEntryAssembly().GetName().Name.ToLower(CultureInfo.InvariantCulture))
    {
        return;
    }
    using var serviceScope = app.ApplicationServices
        .GetRequiredService<IServiceScopeFactory>()
        .CreateScope();
    using var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
    context.Database.Migrate();
}

Migrations

  • Install
# install ef cli tools
dotnet tool install --global dotnet-ef
dotnet tool update --global dotnet-ef
# verify
dotnet ef
  • (set Environmet) - create Migrations
$env:ASPNETCORE_ENVIRONMENT="Local"
dotnet ef migrations add Initial -v

#if running in nuget manager console
Add-Migration Initial -v
  • to update a migration which was already applied you need a rollback:
Update-Database <previous-migration-name> #before the migration you want to update
Remove-Migration
  • or even better Remove last migration with force
Remove-Migration -Force