EFCore.Seeder 2.0.0

A library to seed databases from CSV files, using .NET Core and Entity Framework Core

There is a newer version of this package available.
See the version list below for details.
Install-Package EFCore.Seeder -Version 2.0.0
dotnet add package EFCore.Seeder --version 2.0.0
<PackageReference Include="EFCore.Seeder" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFCore.Seeder --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

EFCore.Seeder

A library to seed databases from CSV files, using .NET Core 2.0 and Entity Framework Core 2.2.1

A project based on dpaquette/EntityFramework.Seeder, using EntityFramework Core instead of EF6, plus some improvements on how to handle resource files.

How to install

Install-Package EFCore.Seeder

How to use

Configuration and setup

This library requires all resource files (CSV files) to be added as Embedded Resource in a runtime available assembly. Once that requirement is met, the seeder needs to be configured using:

SeederConfiguration.ResetConfiguration(assembly: ResourceAssembly);

In order to make it easier, you can reference a type in that assembly and let Reflection take care of retrieving the assembly instance

SeederConfiguration.ResetConfiguration(assembly: typeof(&lt;Assembly Class or Type&gt;).GetTypeInfo().Assembly);

Also, if the format for the CSV files needs to be changed, or the CsvHelper configuration needs some tweaking, this is where we need to pass in the new parameters:

SeederConfiguration.ResetConfiguration(csvConfiguration: csvConfiguration, manifestConfiguration: manifestConfiguration, assembly: typeof(PayrollContext).GetTypeInfo().Assembly);

Where csvConfiguration is an instance of CsvConfiguration, from the CsvHelper library, and manifestConfiguration is an instance of ManifestConfiguration. The last one is just a way to format the embedded resource file names to make them easy to find in the assembly. You can store the configuration in a json file and load it when setting everything up.

"manifestConfiguration": {
        "delimiterFieldName": "{delimiter}",
        "resourceFieldName": "{resource}",
        "extensionFieldName": "{extension}",
        "format": "{delimiter}{resource}{delimiter}{extension}",
        "delimiter": ".",
        "extension": "csv"
    }

Usage

Once the Seeder is configured, all we need to do is call the appropiate extension method when accessing a DbSet for a particular type.

Let's say we have a DbContext with a Products DbSet. We could do the following:

dbContext.Products.SeedDbSetIfEmpty(nameof(context.Products));

This would assume that we have a Products.csv file in the configured assembly, with all the required information to load into the Products entity.

Also, please note that if we're going to update information as well as insert it, the Products entity must implement the IEquatable&lt;T&gt; interface, so we can use product.Equal(other) when finding the right entity to update. This is due to Entity Framework Core not having an AddOrUpdate method (as to this moment, maybe this has changed in EF Core 2.0).

Please check out the test projects for more information on how to use the library.

EFCore.Seeder

A library to seed databases from CSV files, using .NET Core 2.0 and Entity Framework Core 2.2.1

A project based on dpaquette/EntityFramework.Seeder, using EntityFramework Core instead of EF6, plus some improvements on how to handle resource files.

How to install

Install-Package EFCore.Seeder

How to use

Configuration and setup

This library requires all resource files (CSV files) to be added as Embedded Resource in a runtime available assembly. Once that requirement is met, the seeder needs to be configured using:

SeederConfiguration.ResetConfiguration(assembly: ResourceAssembly);

In order to make it easier, you can reference a type in that assembly and let Reflection take care of retrieving the assembly instance

SeederConfiguration.ResetConfiguration(assembly: typeof(&lt;Assembly Class or Type&gt;).GetTypeInfo().Assembly);

Also, if the format for the CSV files needs to be changed, or the CsvHelper configuration needs some tweaking, this is where we need to pass in the new parameters:

SeederConfiguration.ResetConfiguration(csvConfiguration: csvConfiguration, manifestConfiguration: manifestConfiguration, assembly: typeof(PayrollContext).GetTypeInfo().Assembly);

Where csvConfiguration is an instance of CsvConfiguration, from the CsvHelper library, and manifestConfiguration is an instance of ManifestConfiguration. The last one is just a way to format the embedded resource file names to make them easy to find in the assembly. You can store the configuration in a json file and load it when setting everything up.

"manifestConfiguration": {
        "delimiterFieldName": "{delimiter}",
        "resourceFieldName": "{resource}",
        "extensionFieldName": "{extension}",
        "format": "{delimiter}{resource}{delimiter}{extension}",
        "delimiter": ".",
        "extension": "csv"
    }

Usage

Once the Seeder is configured, all we need to do is call the appropiate extension method when accessing a DbSet for a particular type.

Let's say we have a DbContext with a Products DbSet. We could do the following:

dbContext.Products.SeedDbSetIfEmpty(nameof(context.Products));

This would assume that we have a Products.csv file in the configured assembly, with all the required information to load into the Products entity.

Also, please note that if we're going to update information as well as insert it, the Products entity must implement the IEquatable&lt;T&gt; interface, so we can use product.Equal(other) when finding the right entity to update. This is due to Entity Framework Core not having an AddOrUpdate method (as to this moment, maybe this has changed in EF Core 2.0).

Please check out the test projects for more information on how to use the library.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.0 36 9/28/2019
2.1.0 1,215 2/16/2019
2.0.1 84 2/9/2019
2.0.0 98 2/9/2019
1.0.0 476 8/19/2017