1. EF Code First creates a database
Create a new Console application portal and add entityframework through the Package Manager console.
In the Package Manager console, execute the following statement to install EntityFramework.
Pm> Install-package EntityFramework
After successful installation, the interface prompts such as:
Add two entity classes to the new portal console application, with the following code structure:
Where the code for the class file PortalContext.cs is as follows:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Data.Entity;using System.data.entity.infrastructure;using portal.entities;using portal.mapping;namespace Portal{Public class Portalcontext:dbcontext { static portalcontext () { Database.setinitializer (new Dropcreatedatabaseifmodelchanges<portalcontext> ()); } Public dbset<province> Provinces {get; set;} Public dbset<category> Categories {get; set;} protected override void Onmodelcreating (Dbmodelbuilder modelBuilder) { ModelBuilder.Configurations.Add ( New Provincemap ()); MODELBUILDER.CONFIGURATIONS.ADD (New Categorymap ());}}}
In the static constructor, when the database model changes, the current database is deleted and the new database is rebuilt.
After the code executes, the resulting database:
2. EF Code First Database migration
2.1. Build the Database
Modify the static constructor of the class file PortalContext.cs to remove the setting that removes the current database from rebuilding the new database when the database model has changed.
Static Portalcontext () { database.setinitializer<portalcontext> (null);}
1>, in the Package Manager console, execute the statement:
Pm> enable-migrations-enableautomaticmigrations
After successful execution, add the Migrations folder to the Portal console application code structure, and generate the class file Configuration.cs.
namespace portal.migrations{using System; Using System.Data.Entity; Using System.Data.Entity.Migrations; Using System.Linq; Internal sealed class Configuration:dbmigrationsconfiguration<portal.portalcontext> {public Configurati On () {automaticmigrationsenabled = true; } protected override void Seed (Portal.portalcontext context) {//This method would be called aft Er migrating to the latest version. You can use the Dbset<t>. AddOrUpdate () Helper extension method//To avoid creating duplicate seed data. e.g. Context. People.addorupdate (//P = = p.fullname,//new Person {FullName = "Andrew Peters"}, New Person {FullName = ' Brice Lambson '},//new Person {FullName = ' Rowan Miller '} // ); // } }}
2>, in the Package Manager console, execute the statement:
Pm> add-migration Initialcreate
After successful execution, add the class file in the Migrations folder 201309201556388_initialcreate.cs
3>, in the Package Manager console, execute the statement:
Pm> Update-database-verbose
Execution results generate a database consistent with the above
4>, add the city class in the database model, execute the Package Manager console statement, migrations the new class file 201309201643300_addcity.cs in the folder.
Pm> add-migration addcity
Execute the Package Manager console statement again
Pm> Update-database-verbose
Code structure for the Portal console application:
After the database update succeeds, new table city is added to the database.
2.2. Version Backtracking
Modify the table city in the database, and delete the field Provinceno. Execute the following two statements in the Package Manager console:
PM> Add-Migration ModifyCity |
Pm> Update-database-verbose
After successful execution, the city table structure is modified to:
Executes the Package Manager console statement for database version backtracking.
Pm> update-database–targetmigration: "201309201643300_addcity.cs"
2.3. Generate SQL scripts between database versions
Executes a Package Manager console statement that generates a SQL script between the database versions. This operation is generated only for SQL statements and is not executed in the database.
Where-targetmigration is not specified, the default is to migrate to the latest version.
3. Other parameters of the EF Code first migrations statement
1>, enabling database migration for the specified DbContext
Pm> Enable-migrations-contexttypename Portal.portalcontext
2>, setting whether automatic migration is allowed
Enable-migrations
Constructor for the generated Configuration.cs class file
Public Configuration () { automaticmigrationsenabled = false;}
3>, enable-migrations specify the project name
Pm> Enable-migrations-startupprojectname Portal
If you select a default project in package Manager Console, you can not set the-startupprojectname parameter, or you can add the-force parameter if you execute this command more than once.
4>, view the executed SQL statement-verbose directive
EF Code First Migrations Database migration