zoukankan      html  css  js  c++  java
  • .net Core使用EFCore连接数据库

    一、SQL Service

    1.创建实体类

       public class Student
        {
            public int Id { get; set; }
            [Required]
            [Display(Name ="名:")]
            public string FirstName{ get; set; }
            [Required]
            [Display(Name ="姓:")]
            public string  LastName { get; set; }
            [Display(Name ="出生日期:")]
            public DateTime BirthDate { get; set; }
            [Display(Name ="性别:")]
            public Gender Gender { get; set; }
        }

    2.创建DBContext.cs

     public class DataContext:DbContext
        {
            public DataContext(DbContextOptions<DataContext> options):base(options)
            {
            }
    
            public DbSet<Student> Students { get; set; }
        }

    3.打开appsetting.json,添加连接字符串(SqlServer)

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "AllowedHosts": "*",
      "ConnectionStrings": {
        "DefaultConnection": "server=服务器名;database=数据库名;uid=用户名;pwd=密码;" 
      }
    }

    4.打开Startup.cs

           //依赖注入
         private readonly IConfiguration Configuration; public Startup(IConfiguration configuration) { Configuration = configuration; } public void ConfigureServices(IServiceCollection services) {
            //注册服务
           services.AddDbContext
    <DataContext>( options => { options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")); } );      }

    开始迁移。。。。。

  • 相关阅读:
    在一个页面用Ajax访问另一个页面弹出询问框怎么办
    还有一张呢
    系统相关信息查看
    linux dd 命令使用说明
    Github 使用说明
    Hadoop HA 资料
    fedora 在virtualbox安装guest additions
    git 使用记录
    linux console 小技巧
    Agent admitted failure to sign using the key 错误及解决方法
  • 原文地址:https://www.cnblogs.com/mi21/p/10418305.html
Copyright © 2011-2022 走看看