Total Page Preview:   000000001284

What is Migration in Entity Framework Core?

Migration in entity framework core 

Migration in ASP.NET Core refers to the process of managing changes to a database schema over time. It involves creating, modifying, or deleting database tables, columns, and relationships to accommodate changes in your application's data model.
ASP.NET Core provides a feature called Entity Framework Core (EF Core) that simplifies database migrations. With EF Core migrations, you can define changes to your data model in code and then apply those changes to the database without manually writing SQL scripts. This helps keep your database schema in sync with your application's evolving data requirements
In summary, migration in ASP.NET Core involves using tools like Entity Framework Core to automate the process of updating and maintaining your database schema as your application evolves.
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. Whenever you change the domain classes, you need to run migration to keep the database schema up to date.
EF Core migrations are a set of commands which you can execute in NuGet Package Manager Console or in dotnet Command Line Interface (CLI).
The following table lists important migration commands in EF Core.
Adding a Migration
At the very first time, you defined the initial domain classes. At this point, there is no database for your application which can store the data from your domain classes. So, firstly, you need to create a migration.
Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration.

If you are using dotnet Command Line Interface, execute the following command.
In the above commands, MyFirstMigration is the name of a migration. This will create three files in the Migrations folder of your project, as shown below.
 
1-<timestamp>_<Migration Name>.cs: The main migration file which includes migration operations in the Up() and Down() methods. The Up() method includes the code for creating DB objects and Down() method includes code for removing DB objects.
2-<timestamp>_<Migration Name>.Designer.cs: The migrations metadata file which contains information used by EF Core.
3-<contextclassname>ModelSnapshot.cs: A snapshot of your current model. This is used to determine what changed when creating the next migration.
Now, after creating a migration snapshot, it's time to create the database.
Creating or Updating the Database
Use the following command to create or update the database schema.
 
The Update command will create the database based on the context and domain classes and the migration snapshot, which is created using the add-migration or add command.
 
If this is the first migration, then it will also create a table called __EFMigrationsHistory, which will store the name of all migrations, as and when they will be applied to the database.
Removing a Migration
You can remove the last migration if it is not applied to the database. Use the following remove commands to remove the last created migration files and revert the model snapshot.
The above commands will remove the last migration and revert the model snapshot to the previous migration. Please note that if a migration is already applied to the database, then it will throw the following exception.
The migration <migration name> has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.
Reverting a Migration
Suppose you changed your domain class and created the second migration named MySecondMigration using the add-migration command and applied this migration to the database using the Update command. But, for some reason, you want to revert the database to the previous state. In this case, use the update-database <migration name> command to revert the database to the specified previous migration snapshot.

\

The above command will revert the database based on a migration named MyFirstMigration and remove all the changes applied for the second migration named MySecondMigration. This will also remove MySecondMigration entry from the __EFMigrationsHistory table in the database.

Note: This will not remove the migration files related to MySecondMigration. Use the remove commands to remove them from the project.

 

 

 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments

Chong
27-Mar-2019
Great article. I will be going through many of these issues as well..
Caldwell
04-Apr-2019
Everything is very open with a very clear clarification of the issues. It was truly informative. Your website is very useful. Thank you for sharing!
Fortin
28-Mar-2019
It's perfect time to make some plans for the longer term and it's time to be happy. I have read this publish and if I may just I wish to recommend you some attention-grabbing things or advice. Maybe you can write subsequent articles relating to this article. I wish to learn even more issues about it!
Selfe
04-Aug-2019
Hello to every body, it's my first visit of this website; this webpage consists of amazing and in fact excellent material designed for visitors.
navjot
17-Nov-2016
Good Explanation!!!Keep it up:)
Toussaint
12-Nov-2019
Inspiring quest there. What occurred after? Good luck!
Rankin
21-Nov-2019
I've been surfing online more than three hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. In my view, if all site owners and bloggers made good content as you did, the internet will be much more useful than ever before.
Fyans
08-Aug-2020
I like looking through a post that can make people think. Also, thank you for allowing me to comment!
Bertram
21-Nov-2019
Hey there! This is my first comment here so I just wanted to give a quick shout out and say I really enjoy reading your posts. Can you recommend any other blogs/websites/forums that deal with the same subjects? Thanks for your time!
Banner
16-Nov-2019
Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that I acquire actually enjoyed account your blog posts. Anyway I will be subscribing to your augment and even I achievement you access consistently fast.
Whitefoord
20-Nov-2019
First of all I would like to say wonderful blog! I had a quick question that I'd like to ask if you don't mind. I was interested to find out how you center yourself and clear your head prior to writing. I've had difficulty clearing my mind in getting my ideas out there. I do enjoy writing however it just seems like the first 10 to 15 minutes are usually lost simply just trying to figure out how to begin. Any ideas or hints? Thank you!
Ciantar
12-Nov-2019
Hi mates, fastidious article and good urging commented here, I am genuinely enjoying by these.
Mattocks
16-Nov-2019
Hmm is anyone else encountering problems with the pictures on this blog loading? I'm trying to figure out if its a problem on my end or if it's the blog. Any feed-back would be greatly appreciated.
Duras
22-Nov-2019
You have made some decent points there. I looked on the web to learn more about the issue and found most people will go along with your views on this site.
Isaac
11-Jan-2021
Its lіke y᧐u read my mind! Υoᥙ appear to know a lot about this, like yоu wrote the book іn it oг sοmething. I tһink that you can do with some pics to drive the message һome ɑ lіttle bit, Ƅut other than that, thіs is magnificent blog. Ꭺ fantastic read. I'll certaіnly be baсk.
Schiassi
19-Oct-2021
Now I am ready to do my breakfast, afterward having my breakfast coming again to read further news.
Virgin
15-Nov-2020
Hurrah! Finally I got a website from where I know how to in fact obtain helpful data concerning my study and knowledge.
Henning
08-Aug-2020
Amazing! This blog looks exactly like my old one! It's on a totally different subject but it has pretty much the same layout and design. Great choice of colors!
Yang
09-Aug-2020
I am truly thankful to the owner of this web page who has shared this fantastic post at at this place. adreamoftrains webhosting
Catlett
08-Nov-2020
Hello, of course this article is actually good and I have learned lot of things from it on the topic of blogging. thanks.
Steadham
28-Apr-2021
If some one desires to be updated with latest technologies then he must be pay a quick visit this website and be up to date every day.
Loos
27-Apr-2021
Howdy just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Ie. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let you know. The design look great though! Hope you get the problem solved soon. Cheers
Messenger
23-Aug-2021
Hello, I wish for to subscribe for this weblog to obtain hottest updates, so where can i do it please help.
Akers
24-Aug-2021
Piece of writing writing is also a fun, if you be familiar with after that you can write otherwise it is complex to write.
Keeler
24-Aug-2021
Great post however I was wanting to know if you could write a litte more on this subject? I'd be very grateful if you could elaborate a little bit more. Cheers!
Laborde
31-Aug-2021
Everyone loves it when folks come together and share opinions. Great blog, keep it up!
Morris
14-Sep-2021
Hey, I think your blog might be having browser compatibility issues. When I look at your blog in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, amazing blog!
Catt
05-Nov-2021
This is my first time pay a visit at here and i am truly happy to read all at single place.
Crespin
15-Nov-2020
I don't even know how I finished up here, however I assumed this submit was once good. I do not know who you are however definitely you're going to a well-known blogger in the event you are not already. Cheers!
Pontius
30-Apr-2021
I am regular reader, how are you everybody? This piece of writing posted at this web page is really pleasant.
Mattingly
01-May-2021
With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My site has a lot of exclusive content I've either authored myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any techniques to help reduce content from being ripped off? I'd certainly appreciate it.

                           
                           

                           

                           

Facebook User: