![]() |
VOOZH | about |
dotnet add package EFCore.Seeder --version 3.1.0
NuGet\Install-Package EFCore.Seeder -Version 3.1.0
<PackageReference Include="EFCore.Seeder" Version="3.1.0" />
<PackageVersion Include="EFCore.Seeder" Version="3.1.0" />Directory.Packages.props
<PackageReference Include="EFCore.Seeder" />Project file
paket add EFCore.Seeder --version 3.1.0
#r "nuget: EFCore.Seeder, 3.1.0"
#:package EFCore.Seeder@3.1.0
#addin nuget:?package=EFCore.Seeder&version=3.1.0Install as a Cake Addin
#tool nuget:?package=EFCore.Seeder&version=3.1.0Install as a Cake Tool
Due to nuget.org formatting issues, some code sections below are incorrect. You can find the original version of the documentation here
A library to seed databases from CSV files, using .NET Core 3.1 and Entity Framework Core 3.1.3
A project based on dpaquette/EntityFramework.Seeder, using EntityFramework Core instead of EF6, plus some improvements on how to handle resource files.
Install-Package EFCore.Seeder
Embedded ResourceIEquatable<Class> interface. Also, if your entities include an Identity column, make sure to add the attribute [Ignore] to those, or make sure to set IDENTITY_INSERT ON on those tables where you want the tool to insert the identity columns you include in your CSV files.CsvHelper.Configuration.Configuration that conforms to your CSV files. Please refer to their website for more information.ManifestConfiguration so EFCore.Seeder can find your CSV files.SeederConfiguration.ResetConfiguration(csvConfiguration, manifestConfiguration, assembly: typeof(<Assembly>).GetTypeInfo().Assembly); where <Assembly> is the name of one of the classes in the same assembly where the CSV files are included. csvConfiguration and manifestConfiguration are the previously created instances.<DbContext>.<DbSet>.SeedDbSetIfEmpty(nameof(<Resource>));. SeedDbSetIfEmpty is an extension method on DbSet, and <Resource> is the name of the CSV file you want to seed into the database (without the extension ".csv")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(<Assembly Class or Type>).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 CsvHelper.Configuration.Configuration, 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"
}
As written in that example, the library would search for a resource file, in the assembly we indicate, which would be formatted as follows
.<DbSet Entity>.csv
Then the library would get the first ocurrence of a resource which name is as indicated. If we point to a Products manifest, we'd need to name our file Products.csv. Doesn't matter which folder we put it in, or how deep in the hierarchy goes. As all embedded resources would be referenced as <Assembly><Path to Resource><Resource filename>, it would be picked up without problem.
With regards to CsvHelper configuration, please refer to their website.
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<T> 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, so we need to either use the method Add or the method Update.
Please check out the test projects for more information on how to use the library.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 1 NuGet packages that depend on EFCore.Seeder:
| Package | Downloads |
|---|---|
|
Cortside.Common.Testing.EntityFramework
Package Description |
This package is not used by any popular GitHub repositories.