一、开发环境:
VS2015,
.Net Core 1.0.0-preview2-003156
二解决方案:
新建项目:
File --> New --> Project --> ASP.Net Core Web Application(.Net Core) -- > Empty --> OK
添加EF Core引用:
编辑project.json文件,在【dependencies】和【tools】两个节点内添加下面代码
dependencies节点:
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.4", "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
tools节点:
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
添加后结果如图:
添加Model对象及DbContext
User对象:
public class User { public int UserId { get; set; } public string UserName { get; set; } public int Age { get; set; } }
DbContext:
public class UserDbContext:DbContext { public DbSet<User> Users { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=Test;Integrated Security=True"); } }
执行命令生成数据库表:
生成Migrations文件夹及文件:
add-migration Init
结果:
更新数据库生成表:
update-database
结果: