一、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")); } ); }
开始迁移。。。。。