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 }
}
}
Create seed data static class like
update your program.cs code for inserting your seed data like below
In case somebody still interested in this topic, we've created a set of tools (a .net core global tool and a library) which simplifies the process of data seeding.
Long story short: you can save the content of your current DB to some JSON or XML files and then add to your app a few lines of code that loads those files and import the data saved there to your DB. The toolset is totally free and open-source.
The detailed step-by-step instructions are published here.