Objective
In this paper, the so-called data migration, straightforward point as a database upgrade. Although most of the server-based applications, all clients are connected to the same server, for such a production environment, the database upgrade is not difficult, with VS migration, execute SQL script is easier. However, in every customer site to deploy a server is also a lot of applications, if a home to upgrade the database manually, it would be a terrible workload. So how to do this environment to automatically upgrade the database? I believe we also searched the Internet a lot of EF about the production environment of the Data Migration program, and then 99% search is using vs comes with the migration Command mode migration, I do not know why no one to share the production environment of data migration, this is clearly a very important part.
Steps
1. Create an entity
Using System.componentmodel.dataannotations.schema;namespace migration{public class User { [Key, Databasegenerated (databasegeneratedoption.identity)] public Guid Id {get; set;} Public string Name {get; set;} public string Nickname {get; set;} public int Sex {get; set;}} }
2. Create DbContext
Using System.data.entity;namespace migration{ [Dbconfigurationtype (typeof ( MySql.Data.Entity.MySqlEFConfiguration)] public class Dbbase:dbcontext {public dbbase (): Base (" Dbconnect ") { } public dbset<user> Users {get; set;}} }
3, open the Package management console, input pm> Enable-migrations,vs automatically generated the configuration class
Note that the automaticmigrationsenabled value in the generated configuration constructor is false, and we change it to true to enable automatic migration. Then, based on the need to set the database upgrade to allow data loss, it is recommended that the development phase is set automaticmigrationdatalossallowed = true; The first production environment was released, Change it to automaticmigrationdatalossallowed = False, so as not to cause significant customer data loss. Data loss here refers to, for example: the user table, the first version has a Name field, and already have data entry, and then the second version of the Name field is deleted, at this time if automaticmigrationdatalossallowed = False throws an exception and cannot be migrated.
3, modify the DbContext
Using System.data.entity;namespace migration{ [Dbconfigurationtype (typeof ( MySql.Data.Entity.MySqlEFConfiguration)] public class Dbbase:dbcontext {public dbbase (): Base (" Dbconnect ") { //Auto -Migrate Database.setinitializer (new migratedatabasetolatestversion<dbbase, Migration.migrations.configuration> ()); } Public dbset<user> Users {get; set;}} }
The simple three steps to complete the production environment of automatic migration, do not know why this article is not found. The next step is to automatically upgrade the data by replacing the Assembly on the customer site.
EF data Migration in the third production environment