引入包在实体层
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
创建一个Dbcontext
public class TestDbContext : DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
//这里是你创建的实体
public virtual DbSet<Users> Users { get; set; }
}
添加一个Users实体类
public class Users
{
public int ID { get; set; }
public string UserName { get; set; }
}
配置连接字符串(ConfigureServices方法)
services.AddDbContext<Moldes.TestDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("testcontext")));
在有Startup的层安装包
Microsoft.EntityFrameworkCore.Design
打开控制台输入
Add-Migration DBLog
//执行成功后Models会多出的生成文件DBlog
//然后我们在执行命令:“Update-DataBase”,成功后我们查看数据库会自动生成数据库