Analysis on Migration of EF Code First database to Migration, efmigration

Source: Internet
Author: User
Tags custom name

Analysis on Migration of EF Code First database to Migration, efmigration
1. Introduction

The Code First method of Entity Framework provides a method: compile a Model, generate a Model change, and modify the database according to the Model change.

Therefore, the environment is a powerful Nuget. If you are still in VS2010, please do not read it any more. It will be of no benefit.

 

2. Operation step 1) create or modify the Model, that is, the object class;

Here we will demonstrate the modification:

Public class BootStrapLists {public int ID {get; set;} public string Title {get; set;} [DataType (DataType. multilineText)] public string Description {get; set ;}/// <summary> /// Add a time field /// </summary> public DateTime CreateDate {get; set ;}}
2) create or modify ModelMap;
Public class BootStrapListsMap: EntityTypeConfiguration <BootStrapLists> {public BootStrapListsMap () {ToTable (""); HasKey (zw => zw. ID ). property (zw => zw. ID ). hasColumnName ("ID"); Property (zw => zw. title ). hasColumnName ("Title"); Property (zw => zw. description ). hasColumnName ("Description"); // Add Property (zw => zw. createDate ). hasColumnName ("CreateDate ");}}
3) add Map when OnModelCreate;
public DbSet<BootStrapLists> BootStrapLists { get; set; }        protected override void OnModelCreating(DbModelBuilder modelBuilder)        {            modelBuilder.Configurations.Add(new BootStrapListsMap());            //dynamically load all configuration
4) run the Add-Migration command.

If this is the first time you use it, use it first:

Enable-Migrations

Generate Add-Migration Tests

Here, Tests is the custom name;

Update-DataBase

Migration Principles: Query the database table __migrationhistory and traverse all files in the Migrations folder. If the file is not in the _ MigrationHistory table, perform the migration.

 

Eg: If 20141104233562_Tests is not in the database, perform the migration.

11) initial database:

Public partial class Tests: DbMigration {public override void Up () {AddColumn ("dbo. bootStrapLists "," CreateDate ", c => c. dateTime (nullable: true);} public override void Down (){}}

Class, DbMigration has a lot of APIS, you can directly modify the database!


How to Use CodeFirst for data migration

If you are still modifying the model that supports the xxx context after the database is created. Please use Code First to migrate and update the database to find the class library where your database context is located (generally written in the model in the project, and some independent model libraries). Open the Nuget package management console and enter: enable-Migrations Press enter. If yes, Code First migration is enabled for project xxx. Here are several possible errors: 1.No context type was found in the assembly xxx does not find the database context in the current project, that is, the database inherited by DbContext. cs "2.The EntityFramework package is not installed on project xxx the current project has found the data context, but no EntityFrameWork requires installation and input install-package entityframework) if the installation is successful, the Migrations folder appears in the project, which records the changes in each data migration. It is very easy to use, and there is no need to delete the database to regenerate data loss and other issues. Common statement: enable-Migrations-Force replace migration data file update-database update add-migration add new update File

The initialization table data fails when the ef44 code first database is added.

When the seed function content is added, your codefirst code is not updated, so the Seed function is not called.
Migration only checks whether the database organization has changed. If any changes occur, it will call the Seed method to reinitialize the database. However, if the database already exists and is consistent with your current code, Migration will no longer call the seed method.

Related Article

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.