Spyros_Spy
Spyros_Spy

Reputation: 171

OnModelCreating method runs everytime the Application starts

I create a database using code first ef core and I seed it using a migration file by overriding the OnModelCreating method.

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
           : base(options)
    {
    }

    public DbSet<TestModel> TestModel{ get; set; }
    ....

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Seed();
    }

The Seed method reads from several excel files and creates new entities from each row.

modelBuilder.Entity<TestModel>().HasData(
                    new TestModel
                    {
                        Id = int.Parse(reader.GetValue(0).ToString()),
                        ....
                    }

On Startup.cs I get the dbContext and call the Migrate method although I don't think that it is needed since I use the "add-migration" , "update-database" commands before application runs.

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationDbContext applicationDbContext)
    {
        applicationDbContext.Database.Migrate();
        ...
    }

Microsoft docs says that for OnModelCreating method : "Typically, this method is called only once when the first instance of a derived context is created.The model for that context is then cached and is for all further instances of the context in the app domain."

Every time the application starts, the method is called and does it's job. This is the first time experimenting with this and I wouldn't even notice it to be honest if I hadn't had some extension methods that try parse some DateTime values from the files. These break on my laptop since I have different locale on it from the desktop pc.

Does OnModelCreating run on every start up for caching? If so, if a row on the database was updated what happens to the cached row?

Can I make it run only one time(the time it has to seed the data on the migration file) or is there another way for doing that?

Currently I comment in and out those parts when I work from another machine or want to add another migration and I want a workaround. If that is the correct way of all the flow to happen then it means that I have to fix my extension methods so they don't break on other machines.

Upvotes: 1

Views: 1951

Answers (1)

tmaj
tmaj

Reputation: 35135

The alternatives to EF Core's data seeding (which has a number of limitations, see link) which I have used are:

  • seed data as part of the migration aka add code to the migration file.
  • add an extra dedicated migration that only fills in the data - an example is a migration to add a new row to reference like table (available 'enum'-like data).
  • add a function that is called not by EF but by something else - this method contains the code (e.g. LINQ-to-SQL) that adds all necessary data.

Upvotes: 1

Related Questions