创建模型
在Model文件夹下创建UserInfo类,并创建模型
public class UserInfo { [Key] public string openid { get; set; } public string session_key { get; set; } public string nickName { get; set; } public string avatarUrl { get; set; } public int gender { get; set; } public string province { get; set; } public string city { get; set; } public string country { get; set; } }
添加上下文
在Repository文件夹中添加DemoContext类,继承DbContext,添加using System.Data.Entity的引用
public class DemoContex : DbContext { /// <summary> /// 使用“codefirst”连接字符串。根据实际情况修改连接字符串 /// </summary> public DemoContex() : base("data source=127.0.0.1;initial catalog=Demo;user id=sa;password=1234") { } /// <summary> /// code first默认生成的表名会加上s,这段代码生成的数据表名不带s /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } /// <summary> /// 实体类型添加到 DbSet /// </summary> public virtual DbSet<UserInfo> UserInfo { get; set; } }