I have two tables and I want to fill it using seeds.
I use ASP.NET Core 2 in Ubuntu.
How to populate the data for the two tables where one is connected to the other via foreign key?
The Flowmeter has many notes and note belongs to Flowmeter.
I want to do something like this, but it should be stored in the database:
new Flowmeter
{
Make = "Simple model name",
SerialNum = 45,
Model = "Lor Avon",
Notes = new List<Note>()
{
new Note() { Value = 45, CheckedAt = System.DateTime.Now },
new Note() { Value = 98, CheckedAt = System.DateTime.Now }
}
}
As of Entity Framework Core 2.1 there is now a new method of seeding data. In your
DbContext
class overrideOnModelCreating
:And for related entities, use anonymous classes and specify the foreign key of the related entity:
Important: Please note you will need to run an add-migration after you enter this data in your OnModelCreating method and Update-Database to update your data.
The official docs have been updated.
I came across the same question and I fixed the seeding in the following way:
First I added the
public static bool AllMigrationsApplied(this DbContext context)
from garywoodfine to my model.Then I implemented a service scope to seed the db -> see this blog
Then I made a
public static void EnsureSeedData
with the code to generate test data using NBuilder and Faker following the tutorial on this blogI hope this will help people to implement an automated test seed for their projects. Currently I am busy implementing this myself, when I have time I will post some code samples on how to do this.
This is my solution for EF Core 2.0, adapted from https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/#move-database-initialization-code
In program.cs
....
Then my seeder class
I created my seeds in json, and just batch add them my Asp.net core Startup
Very similar to https://garywoodfine.com/how-to-seed-your-ef-core-database/
Did not find an out of the box solution yet.
I don't like the HasData approach than has been written in Microsoft documentation because I cannot keep my migrations clean this way & because
OnModelCreating()
in myDbContext
starts to depend on data which feels a bit wrong and causes issues with random data generator.For me the most efficient and comfortable way is to create a seed class for each of my DbSets that looks like this. (With Bogus library it's as easy as breathing)
Then create the service controller, that works only in development environment.
And call it from Insomnia\Postman whenever I want.
tl;dr: Take a look through my dwCheckApi project to see how I've implemented it.
As others have said, you can read your seed data from JSON or similar (that way it can be source controlled, if you want).
The way that I've implemented it in my projects is to have a method which is called in the
Configure
method in the Startup class (only when in development):which calls the following:
My DbContext is of type
DwContext
which is a class which extends the EF CoreDbContext
typeThe
EnsureSeedData
extension method looks like this:This application is meant to show the relationships between books, characters and series. Which is why there are three seeders.
And one of those seeder methods looks like this:
There's probably some code here which isn't great, but it could be a starting point for you to bounce off of.
Because the seeders are only called when in the development environment, you'll need to ensure that your application starts that way (if starting from the command line you can use
ASPNETCORE_ENVIRONMENT=Development dotnet run
to ensure that it starts in development).It also means that you'll need a different approach to seeding your database in production. In dwCheckApi, I have a controller which can be called to seed the database (take a look at the DatabaseController's SeedData method to see how I do that).