首页 > 其他 > 详细

EF Core CodeFirst

时间:2020-01-31 21:22:00      阅读:116      评论:0      收藏:0      [点我收藏+]

1、 user 实体类

public class User
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public string Pwd { get; set; }

        public string Email { get; set; }

        public DateTime RegistTime { get; set; }

        public DateTime  LastLoginTime { get; set; }

        public bool Status { get; set; }


    }

2、 配置上下文

  public class MyDbContext : DbContext
 {
     public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
     {
     }
     public DbSet<Users> users { get; set; }
 }

3、 配置EF CORE


 public void ConfigureServices(IServiceCollection services)
 {
     services.AddControllers();
     services.AddDbContext<MyDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
 }

4、安装EF core 工具

技术分享图片

因为我使用的是 core 3.1版本,ef core安装的时候版本看你的core的版本吧

5、命令行使用ef core 生成数据库

dotnet ef migrations add Initial     建立并初始化数据库
dotnet ef database update            更新数据库
dotnet ef migrations add xxxx       更新模型字段后需要执行此命令通知vs重新编译表变动  xxxx为变更的任意字段名  一个就够  系统会自动追加变更添加的其他字段
dotnet ef database update           更新数据库

EF Core CodeFirst

原文:https://www.cnblogs.com/qzdd/p/12246720.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!