I am trying seed the database with some data, using ASP.NET CORE 3.0 and EF Core.
I've created my DbContext and according to documentation, online sources, or even EF Core 2.1 questions (I could not find any breaking changes on this topic).
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Band>().HasData(
new Band()
{
Id = Guid.Parse("e96bf6d6-3c62-41a9-8ecf-1bd23af931c9"),
Name = "SomeName",
CreatedOn = new DateTime(1980, 2, 13),
Description = "SomeDescription"
});
base.OnModelCreating(modelBuilder);
}
This does not do what I expect: nothing is seeded on starting the application (even if during debug the method is called from somewhere).
However, if I add a migration, the migration contains the corresponding insert statement (which is not the kind of seeding I am looking for).
Question: What is the right way to have the seed of the database being performed on application start?
By seed the database, I mean that I expect some data to be ensured in some tables everytime the application is started.
I have the alternative to create a seeding class and handle it after the Database.Migrate with custom code, but this seems like a workaround, because the documentation specifies that OnModelCreating should be used to seed data).
So to my understanding after reading the answers and re-reading the documentation, what they mean by "seed" is an "initialization" which can take place right next to the data model (which is why it felt strange - mixing the model creation with the data seeding part).