EF Core Database Migration (migration)

Source: Internet
Author: User

Introduction to Tools and environment

1. Development environment vs 2015

The 2.mysql EF Core supports the POMELO.ENTITYFRAMEWORKCORE.MYSQL source code address (https://github.com/PomeloFoundation/ POMELO.ENTITYFRAMEWORKCORE.MYSQL)

Scene

Design two tables user and posting table (user)

One user corresponds to multiple users

Coding Begin1. Create a new project (Create a new empty console project)

2. Add Nuget.config

Add two feeds, one is pomelo (MySQL EF core support), and the other is NuGet

The code is as follows:

<?xml version="1.0" encoding="utf-8"?>< configuration>  <packageSources>    <add key="pomelo" value="  Https://www.myget.org/F/pomelo/api/v3/index.json" />    <add key="  nuget.org "value="https://www.nuget.org/api/v2 "/ >  </packageSources></configuration>

3. Increase EF core dependency in Project.json while adding EF Tool (for database migrations)
{  "version":"1.0.0-*",  "buildoptions": {    "Emitentrypoint":true  },  "Dependencies": {    "Microsoft.NETCore.App": {      "type":"Platform",      "version":"1.0.0"    },    "POMELO.ENTITYFRAMEWORKCORE.MYSQL":"1.0.0",    "Microsoft.EntityFrameworkCore.Tools":"1.0.0-preview2-final"  },  "Tools": {    "Microsoft.EntityFrameworkCore.Tools":"1.0.0-preview2-final"  },  "Frameworks": {    "netcoreapp1.0": {      "Imports":"Dnxcore50"    }  }}

4. Add User,post entity and DB database context files
The code is as follows:
 Public classUser { Public intId {Set;Get; }  Public stringUserName {Set;Get; }  Public stringPassword {Set;Get; } } Public classpost{ Public intId {Set;Get; }  Public stringTitle {Set;Get; }  Public stringDescription {Set;Get; }  PublicDateTime CreatedDate {Set;Get; }  Public intUserId {Set;Get; }}  Public classDb:dbcontext { PublicDbset<user> Users {Set;Get; }  PublicDbset<post> Posts {Set;Get; } protected Override voidonmodelcreating (ModelBuilder ModelBuilder) {Base.        Onmodelcreating (ModelBuilder); }        protected Override voidonconfiguring (Dbcontextoptionsbuilder optionsbuilder)=Optionsbuilder. Usemysql (@"server=localhost;database=migrationtest;uid=root;pwd=password12!;");} 
5. Generating a database from migration

Enter the following two commands in the Package Manager console in VS
Add-migration Init (execute this command project to generate a directory (migration))
Update-database Init

Before executing

Execute add-migration init (Generate Migration folder)
Execute update-database Init
After the command is executed, the database generates
Post table
6. Inserting data into the database

7. Modify the Entity field, add a field to the post entity, and modify the name of a field

Post-modification posts are as follows

 Public classpost{ Public intId {Set;Get; }  Public stringTitle {Set;Get; }  Public stringHint {Set;Get; }  PublicDateTime CreatedDate {Set;Get; }  Public intUserId {Set;Get; }  Public stringRemark {Set;Get; } }
8. Execute the Migration command

Add-migration UpdateDB
Update-database UpdateDB

Post table after performing the migration

EF Core Database Migration (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.