zoukankan      html  css  js  c++  java
  • .net core2.0入门使用EF

    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了

  • 相关阅读:
    redis哨兵高可用
    数据库主从搭建
    docker 补充
    docker 进阶操作
    docker 简介
    数据可视化(Matplotlib)
    数据操作
    pandas练习
    Pandas简介
    python mysql utf-8 latin
  • 原文地址:https://www.cnblogs.com/Akeke/p/9007845.html
Copyright © 2011-2022 走看看