VS2015,

.Net Core 1.0.0-preview2-003156 

 

二解决方案:

新建项目:

File --> New --> Project -->  ASP.Net Core Web Application(.Net Core) -- > Empty --> OK

 【.Net Core 学习系列】-- EF Core 实践(Code First)

 

添加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"

添加后结果如图:

【.Net Core 学习系列】-- EF Core 实践(Code First)

 

添加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

 

【.Net Core 学习系列】-- EF Core 实践(Code First)

结果:

【.Net Core 学习系列】-- EF Core 实践(Code First)

 

 

 更新数据库生成表:

update-database

 

【.Net Core 学习系列】-- EF Core 实践(Code First)

 

结果:

【.Net Core 学习系列】-- EF Core 实践(Code First)

 

相关文章:

  • 2022-12-23
  • 2018-12-13
  • 2022-12-23
  • 2022-12-23
  • 2019-07-31
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2021-11-07
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案