Akeke
Install-Package Microsoft.EntityFrameworkCore.SqlServer
  • 创建model
    public class User
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
  • appsettings加入ConnetionString

  

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "SqlServer": "Data Source=.;Initial Catalog=WebDemo;User Id=sa;Password=******;"
  }
}

  

  • Startup.cs启动文件给 Context 注入ConnetionString
public void ConfigureServices(IServiceCollection services)
        {
            var connetion = Configuration.GetConnectionString("SqlServer");
            services.AddDbContext<WebDemoContext>(options=>options.UseSqlServer(connetion), ServiceLifetime.Singleton,ServiceLifetime.Singleton);
            services.AddMvc();
        }

  

  • 自定义上下文对象
    public class WebDemoContext : DbContext
    {
        public WebDemoContext(DbContextOptions<WebDemoContext> options):base(options)
        {

        }
        public DbSet<User> User { get; set; }
     
    }

  对数据库进行迁移后,接下来就可以使用 WebDemoContext 对 User 进行CRUD了

分类:

技术点:

相关文章:

  • 2022-02-16
  • 2021-11-12
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2021-05-30
  • 2021-08-18
  • 2021-10-31
  • 2021-06-30
  • 2021-08-28
  • 2021-05-19
  • 2022-01-23
相关资源
相似解决方案