1. Introduction
The Entity Framework's code First approach provides a way to write model models, generate model changes, and modify databases based on model changes.
And since its environment is a strong nuget, if it is VS2010 a classmate, please do not look down, there will be no benefit.
2. Operation Step 1) Establish or modify model, i.e. entity class;
Here is a demonstration of the modification:
Public classbootstraplists { Public intID {Get;Set; } Public stringTitle {Get;Set; } [DataType (Datatype.multilinetext)] Public stringDescription {Get;Set; } /// <summary> ///Add a Time field/// </summary> PublicDateTime CreateDate {Get;Set; } }
2) Establish or modify modelmap;
Public classBootstraplistsmap:entitytypeconfiguration<bootstraplists>{ PublicBootstraplistsmap () {ToTable (""); Haskey (ZW= Zw.id). Property (ZW = Zw.id). Hascolumnname ("ID"); Property (ZW= ZW. Title). Hascolumnname ("Title"); Property (ZW= ZW. Description). Hascolumnname ("Description"); //NewProperty (ZW = ZW). CreateDate). Hascolumnname ("CreateDate"); }}
3) Add map to Onmodelcreate;
Public Get Set ; } protected Override void onmodelcreating (Dbmodelbuilder modelBuilder) { modelBuilder.Configurations.Add (new bootstraplistsmap ()); // dynamically load all configuration
4) using the command add-migration
If you are using it for the first time, first use:
Enable-migrations
The files and folders shown in the red box are generated
To continue using the command:
Add-migration Tests
Where tests is the custom name;
To generate a migration file:
5) using the command update-database
Update-database
Migration Principle : Query the database table, __migrationhistory, traverse all files under the Migrations folder, if the file is not in the __migrationhistory table, then perform the migration.
Eg: If 20141104233562_tests is not in the database, the migration is performed.
11) Initial Database:
22) Execution:
33) After executing the database:
To modify the results of a table:
6) Dig deep
public partial class tests:dbmigration{ public override void up () {ADD Column ( " dbo. bootstraplists ", " createdate " , C = C.datetime (nullable: true public override void down () {}}
Class, Dbmigration mail a lot of API, you can directly modify the database!
Migration Anatomy of EF Code First Database migration