AJProds.EFDataSeeder 1.0.0

Suggested Alternatives

AJProds.EFDataSeeder.MsSql

Additional Details

[BREAKING]: Original package got moved into the AJProds.EFDataSeeder.MsSql package! That project is responsible for the MsSQL migration.

The current packages are:
- AJProds.EFDataSeeder.Core contains the main logic, without migrations
- AJProds.EFDataSeeder.PostgreSQL contains the migration for PostgreSQL, and a reference to the Core
- AJProds.EFDataSeeder.MSSQL contains the migration for MsSQL, and a reference to the Core.

There is a newer version of this package available.
See the version list below for details.
dotnet add package AJProds.EFDataSeeder --version 1.0.0                
NuGet\Install-Package AJProds.EFDataSeeder -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AJProds.EFDataSeeder" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AJProds.EFDataSeeder --version 1.0.0                
#r "nuget: AJProds.EFDataSeeder, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AJProds.EFDataSeeder as a Cake Addin
#addin nuget:?package=AJProds.EFDataSeeder&version=1.0.0

// Install AJProds.EFDataSeeder as a Cake Tool
#tool nuget:?package=AJProds.EFDataSeeder&version=1.0.0                

Problem

There might be situations, when you need to seed some data with keeping the business logic in mind. Applying business logic and versioning of SQL migration scripts can be time-consuming. We should not forget about it would be great to be able to trace back the changes done in the database.

Solution

Let's seed this data with using the existing logic already composed in your solution. Let's do it with simply registering ISeed implementations to your ioc, then let's check in the db, the sdr.SeederHistories table, whether your procedure has been run.

You can define when the ISeed implementations should be run. See the following options.

Options - Mode ⇒ None

No ISeed logic will be run. You need to find the BaseSeederManager in your ioc and run it with the SeedMode.None argument.

var baseSeederManager = provider.GetRequiredService<BaseSeederManager>();
baseSeederManager.Seed(SeedMode.None);

Options - Mode ⇒ BeforeAppStart

You must use the Extensions.MigrateThenRunAsync with your HostBuilder, because this procedure will not start up the host until all migration and seed (with SeedMode.BeforeAppStart) has been run successfully.

await Host.CreateDefaultBuilder()
          .ConfigureServices(ConfigureServices())
          .Build()
          .MigrateThenRunAsync();

Options - Mode ⇒ AfterAppStart

After the host has started successfully, those ISeed classes, that have been set to run AfterAppStart will run in the background, in an IHostedService.

Options - RunAlways ⇒ true

You can re-run the ISeed procedures every time, when the application starts up. Simply set the RunAlways property to true in your ISeed implementation.

Example

  1. Register your ISeed implementations with the Extensions.RegisterDataSeeder<>
    • Here goes your implementations of ISeed
  2. Register this project's tools and services via the Extensions.RegisterDataSeederServices
    • It is needed to use this project's tools
  3. Replace the IHost.StartAsync with Extensions.MigrateThenRunAsync
    • It is needed to be able to run the ISeed with BeforeAppStart option
    • It is needed to ensure the SeederHistories table got migrated

The example is from the AJProds.EFDataSeeder.Tests.Console project

class Program
{
    public const string CONNECTION_LOCAL_TEST =
    @"Server=localhost\SQLEXPRESS;Initial Catalog=SeederTest;Trusted_Connection=True;MultipleActiveResultSets=true";
    
    static async Task Main(string[] args)
    {
        await Host.CreateDefaultBuilder()
                  .ConfigureServices(ConfigureServices())
                  .Build()
                  .MigrateThenRunAsync(provider =>
                                           // Ensure the TestDbContext's migration is run on start
                                           provider.GetRequiredService<TestDbContext>()
                                                   .Database.MigrateAsync());
    }
    
    private static Action<IServiceCollection> ConfigureServices()
    {
        return collection =>
               {
                   // Register seeders
                   collection.RegisterDataSeeder<TestSeed>();
                   collection.RegisterDataSeeder<NewerTestSeed>();
                   
                   // My own, custom, test setup
                   collection.AddDbContext<TestDbContext>(builder => builder
                                                             .UseSqlServer(CONNECTION_LOCAL_TEST));
    
                   // EFSeeder setup - with an own EF Migration History table
                   collection.RegisterDataSeederServices(builder =>
                                                         {
                                                             builder
                                                                .UseSqlServer(CONNECTION_LOCAL_TEST);
                                                         });
               };
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 508 12/5/2021 1.0.1 is deprecated.
1.0.0 430 11/30/2021 1.0.0 is deprecated.