Ef.Seeder
1.0.2
dotnet add package Ef.Seeder --version 1.0.2
NuGet\Install-Package Ef.Seeder -Version 1.0.2
<PackageReference Include="Ef.Seeder" Version="1.0.2" />
paket add Ef.Seeder --version 1.0.2
#r "nuget: Ef.Seeder, 1.0.2"
// Install Ef.Seeder as a Cake Addin #addin nuget:?package=Ef.Seeder&version=1.0.2 // Install Ef.Seeder as a Cake Tool #tool nuget:?package=Ef.Seeder&version=1.0.2
EF.Seeder
Seeder for EFCore and other ORMs.
A new seeder mainly for EFCore and also for other ORMs to easily seeding database.
Previous EFCore seeder:
EFCore has a seeder that can be found in this link. You have to write your seeds in ModelBuilder and seeds will be a part of your migrations what is not an interesting way for seeding the database. Also that seeds won't increase the primary key and indexes of your tables so you have to increase them manually!!!
New Seeder that implemented in this package:
After my experience while using EFCore seeder, I decided to write a new implementation for EFCore seeder. Also a sample project this implementation is available in this repo
New seeder approach:
Define your seeders:
using System.Linq;
using efCoreSeederSample.Models;
using efCoreSeederSample.Seeder.Attributes;
using Microsoft.EntityFrameworkCore;
namespace efCoreSeederSample.Seed
{
public class DatabaseSeed
{
public SeederSampleDbContext DbContext { get; set; }
public DatabaseSeed(SeederSampleDbContext dbContext)
{
DbContext = dbContext;
}
[Seeder(1, typeof(Category)/* this parameter is optional*/)]
public void CategorySeeder()
{
for (var i = 1; i <= 3; i++) {
DbContext.Categories.Add(new Category {
Name = $"Category {i}"
});
}
DbContext.SaveChanges();
}
}
}
As you can see, In this new approach we can easily define a seeder method. Also there is a priority in SeederAttribute
that determines order of seeder methods, this can helps you to seed your database as you defined your relations.
Run your seeders:
- If you want to seed EFCore:
new DatabaseSeeder(ServiceProvider, YourDbContext)
.IsProductionEnvironment(true) // For seed that are needed only in development.
.EnsureSeeded(isNotEfProcess); // For ef-tool processes
- If you want to seed Other ORMs or don't want to pass DbContext:
new DatabaseSeeder(ServiceProvider)
.IsProductionEnvironment(true) // For seed that are needed only in development.
.EnsureSeeded(isNotEfProcess); // For ef-tool processes
You can put above lines in any part of your application, but note that you need to a not disposed ServiceProvider
.
Also you can create a new Command
using AppCommand package
to seed you database without source code. Also with AppCommand package, you can easily access ServiceProvider
SeederAttribute
Parameters:
Priority
:
The priority of the seeder. Assume that because of relations you defined in your database, you can't insert data to table A
before table B
. Now for seeding your database you can set Priority
of BSeeder
to 1
and Priority
of ASeeder
to 2
. Now seeder will run BSeeder
before ASeeder
and you have seed in your both tables.
Type
(Optional):
This is type of the entity you want seed. The model should has a DBSet<>
in your DbContext
.
Note: For other ORMs or other purposes, this parameter is optional and can be null.
Production
(Optional):
When this parameter is true
means this seeder should only run on your production for seeding your database in production environment. And if the parameter is false
, the seeder will only run on development environment.
Force
(Optional):
Seeder automatically checks your database's tables. If the model's table has some data in it, seeder won't run to prevent duplicating data. But with setting Force
Parameter to true
, you can force seeder to insert data again.
Note 1: This parameter is only useful for EFCore orm.
Note 2: This parameter only works when you pass DbContext
object to seeder and Type
is not null.
Purpose:
I think this implementation is good start point for a new seeder in EFCore and it's better than previous seeder of EFCore. I hope you like it.
Donation:
If you like it, you can support me with USDT
:
TJ57yPBVwwK8rjWDxogkGJH1nF3TGPVq98
forUSDT TRC20
0x743379201B80dA1CB680aC08F54b058Ac01346F1
forUSDT ERC20
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. 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. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.