Summary
In development, the development of ways to use EF code first, then if it comes to change data tables, how to do it? Of course, if a new project, delete the database, then rebuild on the line, so if the project is online, the database already contains data, then delete the database to regenerate to die, then how to solve it? Ef provides a data transfer operation. How specific do this operation.
Data migration steps
Development environment vs2017 + Mysql
Here to web, for example. How specific the operation.
1, a new web application.
Found Asp.Net Core site, there are changes in project structure, project structure is as follows:
Wwwroot found right here and moved to the site below, are separated before. This is closer to the original structure asp.net mvc project.
2, installation EF
Use Nuget installed, the following two packets
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools
Pomelo.EntityFrameworkCore.MySql
Tools package, a data migration function, database update operations.
3, adding the test classes and database context
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; namespace Wolfy.MigrationDemo.Models {public class User { [Key] public int Id { set; get; } public string Name { set; get; } public DateTime CreateTime { set; get; } = DateTime.Now; public DateTime ModifyTime { set; get; } = DateTime.Now; } }
Database context
Note: This package needs to be added Pomelo.EntityFrameworkCore.MySql
using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Wolfy.MigrationDemo.Models; namespace Wolfy.MigrationDemo.Data { public class MyContext : DbContext { public DbSet<User> Users { set; get; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseMySql(@"Server=localhost;database=migrationtest;uid=root;pwd=abcd;"); } }
4, by generating a database Migration
In the Nuget vs console, enter the following command
Add-Migration init
Figure
After successful initialization, in solution, the following directory appears
Note: When new projects, preferably in English directory, in the case of the 2.0 version, or when the command is executed, there will be garbled because Chinese, resulting in json format error.
Generate the database
Command is successful, generate the database
After inserting the data migration test
public IActionResult Index() { using (MyContext db = new MyContext()) { db.MyUsers.Add(new Models.User { Name = "Wolfy" }); db.SaveChanges(); } return View(); }
Two, in order to facilitate the statistics, we need the user's age, then we can think User add fields, through data migration, add fields, and will not affect the original data.
FIG entity class, the following changes
public class User { [Key] public int Id { set; get; } public string Name { set; get; } public DateTime CreateTime { set; get; } = DateTime.Now; public DateTime ModifyTime { set; get; } = DateTime.Now; public int Age { set; get; } }
The migration command
PM> Add-Migration init To undo this action, use Remove-Migration. PM> Update-Database init Applying migration '20170917062429_init'. Done. PM> Add-Migration updatedb To undo this action, use Remove-Migration. PM> Update-Database updatedb Applying migration '20170917063252_updatedb'. Done. PM>
View the changes in the data table
to sum up
This article introduces the basic operation of the data migration code first ways.
Transfer: https: //www.imooc.com/article/27276