Synercoding.HostExtensions.EntityFramework 1.0.0-alpha02

A library that contains extensions for Microsoft.Extensions.Hosting.IHost for EF.

This is a prerelease version of Synercoding.HostExtensions.EntityFramework.
Install-Package Synercoding.HostExtensions.EntityFramework -Version 1.0.0-alpha02
dotnet add package Synercoding.HostExtensions.EntityFramework --version 1.0.0-alpha02
<PackageReference Include="Synercoding.HostExtensions.EntityFramework" Version="1.0.0-alpha02" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Synercoding.HostExtensions.EntityFramework --version 1.0.0-alpha02
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Usages

Most of the tasks are build so the can be executed in sync and async code. Before calling Run or RunAsync on the IHost, you can call the different extension methods.

If you are for example running a website using EntityFramework & migrations:

  • You can call ExecuteOnDevelopment (from Synercoding.HostExtensions) with in it a call to MigrateDbContext (from Synercoding.HostExtensions.EntityFramework) and execute the development seed method.
  • If the environment isn't development, but instead testing, then the testseed will be executed.
  • And if non of those apply, only the migration will be executed.
public class Program
{
    public static async Task Main(string[] args)
    {
        await CreateHostBuilder(args)
            .Build()
            .ExecuteOnDevelopment(host => host.MigrateDbContext<MyContext, Migrations.Configuration>(DevelopmentSeeder))
            .ElseIf(
                 host => IsTestingEnvironment(host), 
                 host => host.MigrateDbContext<MyContext, Migrations.Configuration>(TestingSeeder)
             )
            .Else(host => host.MigrateDbContext<MyContext, Migrations.Configuration>())
            .RunAsync();
    }

    private static bool IsTestingEnvironment(IHost host)
        => host.Services.GetRequiredService<IHostEnvironment>().IsEnvironment("Testing");

    private static Task DevelopmentSeeder(MyContext context, IServiceProvider services)
    {
        // Development seed method here
        return Task.CompletedTask;
    }

    private static Task TestingSeeder(MyContext context, IServiceProvider services)
    {
        // Testing seed method here
        return Task.CompletedTask;
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Usages

Most of the tasks are build so the can be executed in sync and async code. Before calling Run or RunAsync on the IHost, you can call the different extension methods.

If you are for example running a website using EntityFramework & migrations:

  • You can call ExecuteOnDevelopment (from Synercoding.HostExtensions) with in it a call to MigrateDbContext (from Synercoding.HostExtensions.EntityFramework) and execute the development seed method.
  • If the environment isn't development, but instead testing, then the testseed will be executed.
  • And if non of those apply, only the migration will be executed.
public class Program
{
    public static async Task Main(string[] args)
    {
        await CreateHostBuilder(args)
            .Build()
            .ExecuteOnDevelopment(host => host.MigrateDbContext<MyContext, Migrations.Configuration>(DevelopmentSeeder))
            .ElseIf(
                 host => IsTestingEnvironment(host), 
                 host => host.MigrateDbContext<MyContext, Migrations.Configuration>(TestingSeeder)
             )
            .Else(host => host.MigrateDbContext<MyContext, Migrations.Configuration>())
            .RunAsync();
    }

    private static bool IsTestingEnvironment(IHost host)
        => host.Services.GetRequiredService<IHostEnvironment>().IsEnvironment("Testing");

    private static Task DevelopmentSeeder(MyContext context, IServiceProvider services)
    {
        // Development seed method here
        return Task.CompletedTask;
    }

    private static Task TestingSeeder(MyContext context, IServiceProvider services)
    {
        // Testing seed method here
        return Task.CompletedTask;
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Release Notes

- Added overloads for ElseExecuteHost, IHost and THost

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0-alpha02 85 5/18/2020
1.0.0-alpha01 60 5/18/2020