1.开发环境为vs 2015
2.mysql EF Core支持采用 Pomelo.EntityFrameworkCore.MySql 源代码地址(https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql)
设计两张表 用户表(user)和发帖表(user)
一个用户对应多个用户
增加两个feed,一个是Pomelo(mysql ef core的支持),一个是nuget
代码如下:
<?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>
{ "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" } } }
代码分别如下:
public class User { public int Id { set; get; } public string UserName { set; get; } public string Password { set; get; } } public class Post { public int Id { set; get; } public string Title { set; get; } public string Description { set; get; } public DateTime CreatedDate { set; get; } public int UserId { set; get; } } public class DB : DbContext { public DbSet<User> Users { set; get; } public DbSet<Post> Posts { set; get; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseMySql(@"Server=localhost;database=migrationtest;uid=root;pwd=Password12!;"); }
在vs中的“程序包管理器控制台”中输入如下两个命令
Add-Migration init(执行此命令项目生成一个目录(Migration))
Update-Database init
执行之前
执行Add-Migration init(生成Migration文件夹)
执行 Update-Database init
执行命令之后,数据库生成
Post表
修改之后的post如下
public class Post { public int Id { set; get; } public string Title { set; get; } public string Hint { set; get; } public DateTime CreatedDate { set; get; } public int UserId { set; get; } public string Remark { set; get; } }
Add-Migration updatedb
Update-Database updatedb
执行迁移之后的post表
原文:http://www.cnblogs.com/nele/p/5831434.html