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")); } );      }

    开始迁移。。。。。

  • 相关阅读:
    会跳舞的树(只用HTML+CSS)(转)
    国内UED收录
    HDU 1078 dfs+dp
    HDU 1278
    HDU 4499
    HDU 4597
    POJ2777
    POJ1780 Code
    简单的Fleury算法模板
    POJ 2513 无向欧拉通路+字典树+并查集
  • 原文地址:https://www.cnblogs.com/mi21/p/10418305.html
Copyright © 2011-2022 走看看