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):
if (env.IsDevelopment())
{
app.EnsureDatabaseIsSeeded(false);
}
which calls the following:
public static int EnsureDatabaseIsSeeded(this IApplicationBuilder applicationBuilder,
bool autoMigrateDatabase)
{
// seed the database using an extension method
using (var serviceScope = applicationBuilder.ApplicationServices
.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<DwContext>();
if (autoMigrateDatabase)
{
context.Database.Migrate();
}
return context.EnsureSeedData();
}
}
My DbContext is of type DwContext
which is a class which extends the EF Core DbContext
type
The EnsureSeedData
extension method looks like this:
public static int EnsureSeedData(this DwContext context)
{
var bookCount = default(int);
var characterCount = default(int);
var bookSeriesCount = default(int);
// Because each of the following seed method needs to do a save
// (the data they're importing is relational), we need to call
// SaveAsync within each method.
// So let's keep tabs on the counts as they come back
var dbSeeder = new DatabaseSeeder(context);
if (!context.Books.Any())
{
var pathToSeedData = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookSeedData.json");
bookCount = dbSeeder.SeedBookEntitiesFromJson(pathToSeedData).Result;
}
if (!context.BookCharacters.Any())
{
characterCount = dbSeeder.SeedBookCharacterEntriesFromJson().Result;
}
if (!context.BookSeries.Any())
{
bookSeriesCount = dbSeeder.SeedBookSeriesEntriesFromJson().Result;
}
return bookCount + characterCount + bookSeriesCount;
}
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:
public async Task<int> SeedBookEntitiesFromJson(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentException($"Value of {filePath} must be supplied to {nameof(SeedBookEntitiesFromJson)}");
}
if (!File.Exists(filePath))
{
throw new ArgumentException($"The file { filePath} does not exist");
}
var dataSet = File.ReadAllText(filePath);
var seedData = JsonConvert.DeserializeObject<List<Book>>(dataSet);
// ensure that we only get the distinct books (based on their name)
var distinctSeedData = seedData.GroupBy(b => b.BookName).Select(b => b.First());
_context.Books.AddRange(distinctSeedData);
return await _context.SaveChangesAsync();
}
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).