EF Code First and migration were used in the project, but some aspects were found to be indefinitely. For example: How to control the migration process in the migration file? What if the SQL statement is executed in the migration file? How do I use other parameters of update-database? How does the database migrate in the production environment? So, here are some of these experiences:
Enable-migration
Used when building the database for the first time.
→enable-migration
→ More migrations folders under the class library, including Configuration.cs and each migration log file
Each migration file is roughly the following:
Public Partial classinitialcreae:dbmigration{ Public Override voidUp () {createtable ("Aliases", C=New{Id= C.int (Nullable:false, Identity:true), Name= C.string (Nullable:false), UserName= C.string (maxLength: -), Email=c.string (), Bio=c.string (), CreateDate= C.datetime (Nullable:false) } ) . PrimaryKey (t=t.id); CreateTable ("Tweets", C=New{Id= C.int (Nullable:false, Identity:true), CreateDate= C.datetime (Nullable:false), Aliasid= C.int (Nullable:false) } ) . PrimaryKey (t=t.id). ForeignKey ("Aliases", t = t.aliasid, Cascadedelete:true) . Index (t=t.aliasid); } Public OverrideDown () {Dropindex ("Tweets",New[]{"Aliasid"}); Dropforeignkey ("Tweets","Aliasid","Aliases"); Droptable ("Tweets"); Droptable ("Aliases"); }}
The migrated configuration files are roughly:
Internal Sealed class Configuration:dbmigrationsconfiguration<tweetercontext>{ public Configuration () { false; } protected Override void Seed (twittercontext context) { }}
Database initialization
Database.setinitializer (New Migratedatabasetolastesverstion<twittercontext, configuration> ());
Automatic or manual migration settings
Public class Configuration:: dbmigrationsconfiguration<tweetercontext>{ public configuration ( { // If manually migrated, set to False true; // even if there is data in the column, this column can be deleted, but only when the column data is missing. true ; }}
Add-migration
Each migration is saved locally.
The configuration file is roughly as follows:
Public class Configuration:dbmigrationsconfiguration<twittercontext>{ public Configuraiton () { false; true ; } protected Override void Seed (twittercontext context) { }}
→ For example, add a column
→ Run: add-migration somename
→ Migration files are roughly
Public Partial classaddsomecolumntosometable:dbmigration{ Public Override voidUp () {AddColumn ("sometable","Somecolumn", C =c.string ()); } Public Override voidDown () {Dropcolumn ("sometable","Somecolumn"); }}
Update-database
Updates the database and executes the migrated files that were not executed.
→ Execution: update-database-verbose
Verbose represents an update detail that shows which migrations were performed and which SQL statements were executed.
→ Other parameters
Targetmigration
Script & sourcemigration: Creating SQL statements
Force: Allow data loss
The class library where the Projectname:dbmigrationsconfiguraiton is located
Inheriting classes of Configurationtypename:dbmigrationsconfiguration
Sartupprojectname: The class library where the connection string resides
ConnectionString & Connectionprovidername: Explicit connection string or provider
or by: Get-help update-database-detailed, learn more.
Rolling back
Update-database-targetmigration: "Somemigrationname"-verbose
Customize some migration actions in the migration file to control the migration process
All migrated files inherit from the Dbmigration class, which provides many ways for us to control the migration process.
→ For example, let the added class have a default value
Public Partial classaddsomecolumn:dbmigration{ Public Override voidUp () {AddColumn ("sometable","Somecolumn", C = c.string (defaultvalue:"sth")); } Public Override voidDown () {Dropcolumn ("sometable","Somecolumn"); }}
Execute SQL statement
For example, some columns of existing data are NULL, what if you want to attach a value to the column for those rows?
→ Change the domain model
→ Execution: add-migration somename
→ Execute SQL statements in the migration file
Public Partial classaddsome:dbmigration{ Public Override voidUp () {AddColumn ("Table","column", C = c.string (maxLength:Ten)); SQL ("Update Table Set column= ' Sth '"); } Public Override voidDown () {Dropcolumn ("Table","column"); }}
→update-database-verbose
Seed data for a database
Public class Configuration:dbmigrationsconfiguraiton<twittercotext>{ public Configuration () { false; true ; } protected Override void Seed (twittercontext context) { newnew ALias () }}}
migrating databases in a production environment
→ Execution: udb-script-verbose
→ Pop up a window that can write SQL statements without connecting to the database at this time
ALTER TABLE sometable add column nvarchar (max)
Insert into _migrationhistory (Migrationid,createdon, Model, productversion) VALUES (...)
→ Execution: udb-verbose
When you connect to the database, execute the statement
→ If you want to start from one migration
Udb-sourcemigration: "Somename"-script
→ Execution: udb-verbose
Close seed data
Database.setintializer<somecontext> (NULL);
Entity Framework Code First Migration Fundamentals supplements