Migration Anatomy of EF Code First Database migration

Source: Internet
Author: User
Tags custom name

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.