EntityFramework 学习 一 DbContext
上一节中EDM自动生成SchoolEntities类,该类继承DbContext
EntityFramework4.1之前的版本,EDM生成的类继承ObjectContext,使用ObjectContext稍微有点棘手,DbContext概念上与ObjectContext相似,它是ObjectContext的封装,DbContext是EF重要的组成部分,它是领域或实体类和数据库的桥梁
DbContext是主要的类负责数据和对象互相转化
EntitySet: DbContext包含实体集合(DBSet<TEntity>),所有与数据库表对应的实体
Querying: DbContext将LINQ-to-Entity查询语言转化成SQL查询语言,并发送给数据库
Change Tracking: 当对象已经从数据库中查询到后,DbContext跟踪实体的变化情况
Persisting Data: DbContext执行插入、更新、删除操作
Caching: DbContext默认使用一级缓存,在一个context对象周期内,它缓存所有已经被查询的实体
Manage Relationship: DbContext管理对象间的关系
namespace EFTutorials { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.Core.Objects; using System.Linq; public partial class SchoolDBEntities : DbContext { public SchoolDBEntities() : base("name=SchoolDBEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Course> Courses { get; set; } public virtual DbSet<Standard> Standards { get; set; } public virtual DbSet<Student> Students { get; set; } public virtual DbSet<StudentAddress> StudentAddresses { get; set; } public virtual DbSet<Teacher> Teachers { get; set; } public virtual DbSet<View_StudentCourse> View_StudentCourse { get; set; } public virtual ObjectResult<GetCoursesByStudentId_Result> GetCoursesByStudentId(Nullable<int> studentId) { var studentIdParameter = studentId.HasValue ? new ObjectParameter("StudentId", studentId) : new ObjectParameter("StudentId", typeof(int)); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetCoursesByStudentId_Result>("GetCoursesByStudentId", studentIdParameter); } public virtual int sp_DeleteStudent(Nullable<int> studentId) { var studentIdParameter = studentId.HasValue ? new ObjectParameter("StudentId", studentId) : new ObjectParameter("StudentId", typeof(int)); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_DeleteStudent", studentIdParameter); } public virtual ObjectResult<Nullable<decimal>> sp_InsertStudentInfo(Nullable<int> standardId, string studentName) { var standardIdParameter = standardId.HasValue ? new ObjectParameter("StandardId", standardId) : new ("StandardId", typeof(int)); var studentNameParameter = studentName != null ? new ObjectParameter("StudentName", studentName) : new ObjectParameter("StudentName", typeof(string)); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<decimal>>("sp_InsertStudentInfo", standardIdParameter, studentNameParameter); } public virtual int sp_UpdateStudent(Nullable<int> studentId, Nullable<int> standardId, string studentName) { var studentIdParameter = studentId.HasValue ? new ObjectParameter("StudentId", studentId) : new ObjectParameter("StudentId", typeof(int)); var standardIdParameter = standardId.HasValue ? new ObjectParameter("StandardId", standardId) : new ObjectParameter("StandardId", typeof(int)); var studentNameParameter = studentName != null ? new ObjectParameter("StudentName", studentName) : new ObjectParameter("StudentName", typeof(string)); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_UpdateStudent", studentIdParameter, standardIdParameter, studentNameParameter); } } }
DbContext实例化,用来执行GRUD操作
using (var ctx = new SchoolDBEntities()) { //Can perform CRUD operation using ctx here.. }
从DbContext中获取ObjectContext
DbContext API比ObjectContext用起来简单些,然而,你可以从DbContext中得到ObjectContext的引用,
using (var ctx = new SchoolDBEntities()) { var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext; //use objectContext here.. }