Entity Framework Code First Migrations Interview Questions And Answers

In this tutorial i am going to explain about . In my previous tutorial i have explained about Entity Framework Code First Migrations Interview Questions And Answers



1.How to apply all pending migrations automatically while deploying your application?
Using MigrateDatabaseToLatestVersion Initializer

2.How to enable code first migrations in Entity Framework?
Run the Enable-Migrations command in Package Manager Console

3. What is the use of Add-Migration command?
Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created.

4.What is the use of Update-Database command?
Update-Database will apply any pending migrations to the database

5. Use of –Verbose command with Update-Database
To see the SQL query that Code First Migrations is running.

6. How to migrate to a Specific Version in EF Code first?
Run the Update-Database –TargetMigration: <MigrationName> command in Package Manager Console.

7. How to empty the database in EF Code first?
You can use the Update-Database –TargetMigration: $InitialDatabase command.

8. How to update to a specific version in EF code first?
Run the Update-Database –TargetMigration: <MigrationName> command in Package Manager Console.

9. How to generate migration SQL script in EF code first?
Run the Update-Database command but with –Script flag so that changes are written to a script rather than applied.

Example:

Update-Database -Script -SourceMigration: <SourceMigrationName> -TargetMigration: <TargetMigrationName>

If you specify SourceMigration as $InitialDatabase then it will generate script from empty database to TargetMigrationName.

If you don’t specify a target migration, Migrations will use the latest migration as the target.

If you don’t specify a source migrations, Migrations will use the current state of the database.

10. How to enable Automatic Migrations in EF code first?
Run the Enable-Migrations –EnableAutomaticMigrations command in Package Manager Console

11. How to Use existing schema as a starting point in EF code first migrations?
Run the Add-Migration InitialCreate –IgnoreChanges command in Package Manager Console. This creates an empty migration with the current model as a snapshot.

Run the Update-Database command in Package Manager Console. This will apply the InitialCreate migration to the database. Since the actual migration doesn’t contain any changes, it will simply add a row to the __MigrationsHistory table indicating that this migration has already been applied.

12. What is Migrations History Table?
Migrations history table is a table used by Code First Migrations to store details about migrations applied to the database

13. Can we customize Migrations History Table?
Yes

14. How to resolve conflicts in Code First Migrations in Team Environments?
Refer below microsoft link for details.

https://docs.microsoft.com/en-gb/ef/ef6/modeling/code-first/migrations/teams

Conclusion:

Hope you learned Entity Framework Code First Migrations Interview Questions And Answers.  Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Leave a Reply

Your email address will not be published. Required fields are marked *