How to use Entity Framework Migrations | Code First approach | .net core MVC code first

Summary: As you know in the Entity framework we have many ways to connect with the database from our application. We are using their Model first approach, Database first approach, and Code First approach.
All approaches have different ways to use or Different requirement to use. The main thing is why we use this or which one we use its a long story we will discuss in another article.
In today’s article, we will discuss Code First approach and we will learn about it all.

Why we use:

  1. A developer can handle without much database knowledge.
  2. A developer can focus on the Coding part only.
  3. What we did daily bases on the proper history we have as in the form of Migrations.
  4. In models, we created relationships so we can easy to use the Lazy Loading of the child table.
  5. No need for convert naming, Which we choose in our Domain models that field naming will be used in a database table.
  6. If something will change in the Table, we will make changes only code side. no need to touch the database.
  7. Easy or automatic version control of the database.
  8. In migration Up and Down have all history, what you going to do now or what was old, so in future, we can easy to rollback to our previous migration.
  9. The conclusion is everything is on a single hand.

How to use it or Issues and solution.

All we will learn with a video demo. It will help you to understands better.
Video link
Subscribe to this channel video will be upload there.

Entity Framework Migrations Code First approach .net core MVC code first

Most useable command:

We can use it to enable migrations in our project.

>Add-migration NameOfMigration
After creating a model or after making changes in the Model we are add-migration command to create new migration for updates or new in the table.

Suppose we are not satisfied with created migration we need to change something in the model then we can use this command to remove the lastly created migration.

It uses to commit all created migration to our SQL Server Database. After running this what we did in models for a created table we can see in our database.

>Update-database MigrationNameWhereYouWantToRollback
This command is used if we want to roll back to previously created migration. Sometimes we have some issues in lastly created migration which is also committed to our database. the issue will be anything, but in this case, we better know to which migration my project was running fine, So we can easy to rollback to the previous migration.

>Script-Migration -From FromMigrationName -To ToMigrationName
This command is used to get SQL script, it shows us in SQL query form what migration did in particular migration.
we can also get a single migration script or also from to To migration SQL script by using this.