- DbContext
1.指定连接字符串(上一章提到)
public string ConnectionStringName { get; private set; } /// <summary> /// 构造方法 /// </summary> /// <param name="connectionStringName">数据库连接字符串名称</param> protected EntityContext(string connectionStringName) : base(connectionStringName) { ConnectionStringName = connectionStringName; }
public StoreProceContext(): base("StoreProce"){}
如果connectionStrings节点中有name为StoreProce的节点,取webConfig中的配置连接
如果没有,就一StoreProce为表名配置连接字符串
2.注册实体类
public DbSet<Goods> Goodses { get; set; } public DbSet<Students> Studentses { get; set; }
将Goods和Students实体类注册到DbSet中
3.配置文件中添加实体类和表映射关系类(Map)
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new GoodMap()); modelBuilder.Configurations.Add(new StudentsMap()); }