In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the EF architecture codeFirst from initialization to database migration process is how, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Some introductions
CodeFirst is an EntityFrameworks development model, that is, code first, it is based on business code, through the code to generate the database, and coupled with the powerful data table comparison function of migration to generate the database version, so that program developers do not have to maintain changes to the database, but directly maintain migration, in which there are your current version and past historical version of all the change records!
Version control of Migration database
(1) through EF-CodeFirst, we set up the database and initialize the data table.
Establish the data model for the first time, run the program, build the database and data table automatically, and perform the corresponding initialization work.
Data model
Data initialization
Registration of EF in LindAgile
The database is created for the first time, and the name of the data table name can be controlled in both singular and plural forms.
Protected override void OnModelCreating (DbModelBuilder modelBuilder) {/ / Note that when the form is plural, the entity cannot be underlined and must be the word modelBuilder.Conventions.Remove ();}
Among them, _ _ MigrationHistory table, which is a data table of data changes, reflects the history of the database.
(2) start data migration
At this point, we see that there is no migrations folder in the solution, because you do not open the change plan at this time.
This folder will not appear until the changes are enabled.
Enable-Migrations
Then you see the migrations folder in the project, which will store the change records of our database.
If we add a new data entity DO, or modify the original entity, and then want to upgrade a change record, we can use add-migration to change the name (write whatever)
Then there will be an extra change file in our project's Migrations directory, which starts with a timestamp, which is the key to its sequential execution when the change is executed.
(3) update the changes of the model to the database
At the same time, we look at a data table _ _ MigrationHistory, which has not changed, because we have not updated the changes to the database. If you want to update to the database, you can use update-database. If you do not add the version number, the default update is the latest change record, which will include the previous change code. When it is updated, it will be compared with the change record in the data table _ _ MigrationHistory, if it has already been updated. This version will not be executed, but will only be updated backward!
After the execution is completed, we will take a look at the changes in the data table. it will also have an up-to-date change record. If we change the data table later, we can use this method. As long as we make good use of EF-migration, it will only make our development faster!
(4) change the model into a SQL script for use in the production environment
Update-Database-Script-SourceMigration: $InitialDatabase-TargetMigration:init on the EF architecture codeFirst from initialization to database migration process is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.