Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and where to call Database.EnsureCreated and Database.Migrate?

I have a ASP.NET MVC 6 application, and i need to call the Database.EnsureCreated and Database.Migrate methods.

But where should I call them?

like image 812
bailando bailando Avatar asked Jul 07 '16 05:07

bailando bailando


People also ask

What is database EnsureCreated?

EnsureCreated. EnsureCreated will create the database if it doesn't exist and initialize the database schema. If any tables exist (including tables for another DbContext class), the schema won't be initialized.

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.

What is database migration in asp net core?

Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.


1 Answers

I think this is an important question and should be well answered!

What is Database.EnsureCreated?

context.Database.EnsureCreated() is new EF core method which ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created and also it ensures it is compatible with the model for this context.

Note: This method does not use migrations to create the database. In addition, the database that is created cannot later be updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is created and all migrations are applied.

How did we do that with EF 6?

context.Database.EnsureCreated() is equivalent to the below listed approaches of EF 6:

  1. Package Manager Console:

    Enable-Migrations -EnableAutomaticMigrations. Add-Migration/Update-Database.

  2. From code:

    Database.SetInitializer CreateDatabaseIfNotExists

or

With DbMigrationsConfiguration and set AutomaticMigrationsEnabled = true;

What is Database.Migrate?

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

How did we do that with EF 6?

context.Database.Migrate() is equivalent to the below listed approaches of EF 6:

  1. Package Manager Console:

    Update-Database -TargetMigration

  2. With a custom DbMigrationsConfiguration:

    AutomaticMigrationsEnabled = false; or with DbMigrator.

Conclusion:

If you are using migrations there is context.Database.Migrate(). If you don't want migrations and just want a quick database (usually for testing) then use context.Database.EnsureCreated()/EnsureDeleted().

like image 123
Bassam Alugili Avatar answered Oct 06 '22 06:10

Bassam Alugili