In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize the EF Core migration of ASP.NET". In the daily operation, I believe that many people have doubts about how to achieve the EF Core migration of ASP.NET. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve the EF Core migration of ASP.NET". Next, please follow the editor to study!
When you develop a new application, your model changes frequently, and each change in the data model will make it out of sync with the database. You configure EF Core to create a database when it does not exist. Each time you change the data model (add or delete entity classes or change DbContextClass), you can delete the database and recreate a database that matches the data model, and then populate the test data.
This method of keeping the data model synchronized with the database works well, but then does not apply to the phase of releasing the application to the product. When an application is in a real product, the product usually contains data that you need to retain, and when the model changes (such as adding a new column), you don't want to lose any data. The EF Core migration feature solves this problem by enabling EF to update the structure of the database without having to recreate a new database.
To be able to use migration, you can use Package Manager Console (PMC) or command-line interface (CLI command line).
The EF tool for CLI is provided in the Microsoft.EnrityFrameworkCore.Tools toolkit, and the version of vs 15.7 has been added to the Microsoft.AspNetCore.App package in the MVC template by default. You don't have to add it manually, as introduced in the official tutorial.
Change the connection string
Change the database name in the database connection string in the appsettings.josn file to a previously unused name. This change enables the project to create a new database using first migration. This is not a necessary operation to start using migration, but you will see that it is a good idea.
Another way is to delete the database from SSOX (SQL Server Object Exporer SQL object Explorer) or CLI.
The CLI command to delete the database is: dotnet ef database drop
Create initialization migration
In solution Manager, right-click the project folder-Select Open in File Explorer from the pop-up menu-- enter cmd.
Enter: dotnet ef migrations add InitialCreate in the console window
The code generated by EF creates the database from scratch.
A Migrations folder is created in the solution Manager with a cs file with the same name and timestamp. The Up method creates the database tables corresponding to the entity set of the data model, and the down method deletes them.
If you create an initialization migration, the database already exists. The code to create the database will still be generated but it will not run because the database already matches the data model. When you release an application to another environment, the database does not exist, and this code will create the database, so it is a good idea to test it first.
Data model snapshot (Data Model snashot)
The migration creates a snapshot of the existing database structure. When you add a migration, decide what has changed by comparing the data model with the snapshot file.
The console command to delete the migration is dotnet ef migrations remove, which deletes the migration and ensures that the snapshot is correctly reset.
Application migration to database
CLI:dotnet ef database update that updates the database by applying migration
The EFMigrationsHistory table in the database tracks which migrations are applied to the database.
At this point, the study on "how to achieve EF Core transfer of ASP.NET" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.