- 新建web应用程序
- 新建DAL文件夹,添加新建项>Data>ADO.NET实体数据模型DBContext
- 上一步操作得到以下结果:
- 自动添加EntityFramework引用
- Web.Config中添加数据连接
- 添加数据访问上下文
- 以及数据库 Model
<connectionStrings>
<add name="DBConn" connectionString="data source=.;initial catalog=ClayDataBase;persist security info=True;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
public partial class DBContext : DbContext
{
public DBContext() : base("name=DBConn") { }
public virtual DbSet<Customer> Customer { get; set; }
public virtual DbSet<Product> Product { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { }
}