0

I want my .NET 7 app to boot up and create a database if one does not exist.

I am using Entity Framework Core.

There seems to be 2 conflicting methods:

Option #1:

context.Database.EnsureCreated()

I'm told this method will not work with database migrations, and it is difficult to add them to a database created this way later on. Seems to be popular. Maybe used for testing?

Option #2:

context.Database.Migrate()

This should just run your existing database migrations right?

All the professional app's I've been involved with used migrations, so it seems like this is the way to go.

Also, where do we add this code? Inside entity's DbContext or inside Program.cs ?

Which option is better and what code would you use to implement it?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Scottish Smile
  • 455
  • 7
  • 22
  • 1
    Does this answer your question? [How and where to call Database.EnsureCreated and Database.Migrate?](https://stackoverflow.com/questions/38238043/how-and-where-to-call-database-ensurecreated-and-database-migrate) – Mohammad Aghazadeh Aug 05 '23 at 06:45
  • A little but the only full section of code given is for .Net Core inside the Startup Class that doesn't exist in .Net 7. – Scottish Smile Aug 05 '23 at 12:26

2 Answers2

2

They are for different goals, EnsureCreated is used for initial creation (usually I use it after EnsureDeleted on local dev environment to recreate from scratch). From the docs:

-If the database exists and has any tables, then no action is taken. Nothing is done to ensure the database schema is compatible with the Entity Framework model. -If the database exists but does not have any tables, then the Entity Framework model is used to create the database schema. -If the database does not exist, then the database is created and the Entity Framework model is used to create the database schema

And :

Note that this API does not use migrations to create the database. In addition, the database that is created cannot be later updated using migrations.

If the goal is to manage changes (via migrations) and to bring the database to the current state then Migrate is your choice . From the docs:

Applies any pending migrations for the context to the database. Will create the database if it does not already exist.

Note that this API is mutually exclusive with EnsureCreated(). EnsureCreated does not use migrations to create the database and therefore the database that is created cannot be later updated using migrations.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
2

Based on this link, complete explanations are provided. This is the answer for this part of your question

"Also, where do we add this code? Inside entity's DbContext or inside Program.cs ?"

You can add it in the program.cs file as shown below:

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
   //replace DataContext with your Db Context name
   var dataContext = scope.ServiceProvider.GetRequiredService<DataContext>();
   dataContext.Database.Migrate();
}

or cleaner way is create an Extension Method :

first create class as follows :

public static class PrepDb
{
    public static void ApplyMigration(this IApplicationBuilder app)
    {
        using var scope = app.ApplicationServices.CreateScope();
        //replace DataContext with your Db Context name
        var dataContext = scope.ServiceProvider.GetRequiredService<DataContext>();
        dataContext.Database.Migrate();
    }
}

and in program.cs file use as follows :

 var app = builder.Build();

 app.ApplyMigration();
Mohammad Aghazadeh
  • 2,108
  • 3
  • 9
  • 20