Entity Framework tutorial -- DBContext, entitydbcontext
DBContext:
In the previous section "create an object data model", EDM creates a SchoolDBEntities class for us.System. Data. Entity. DbContextThis class. This DbContext is called the context class in EF.
Before EF4.1, the context class generated by EDM is derived from the ObjectContext class. It is a bit difficult to use. DbContext is similar to ObjectContext in concept. DbContext only encapsulates ObjectContext to make it easier to use in all development scenarios. (Such as Code First, Model First, and Database First)
DbContext is an important part of EF. It serves as a bridge between the database and your application domain or entity class.
DbContext is the main type responsible for data and object interoperability. It is mainly responsible for the following actions:
EntitySet:DbContext contains a set of all entity objects mapped from the database table (such as DbSet <TEntity> ).
Querying:DbContext converts LINQ To Entities into SQL query statements and sends them To the database.
Change Tracking:It keeps track of changes. Once an object changes, it will be queried from the database.
Persisting Data:It can also insert, update, and delete databases based on the entity status.
Caching:DbContext is used as the first-level cache by default. It stores object retrieved in the context class lifecycle.
Manage Relationship:In DB-First or Model-First, DbContext uses CSDL, MSL, and SSDL to manage relationships, and uses stream APIs to manage relationships in Code-First.
Object Materialization:DbContext converts the original table data to an object.
In the following example, the SchoolDBEntities class is created by EDM Based on the SchoolDB database.
1 namespace EFTutorials 2 { 3 using System; 4 using System.Data.Entity; 5 using System.Data.Entity.Infrastructure; 6 using System.Data.Entity.Core.Objects; 7 using System.Linq; 8 9 public partial class SchoolDBEntities : DbContext10 {11 public SchoolDBEntities()12 : base("name=SchoolDBEntities")13 {14 }15 16 protected override void OnModelCreating(DbModelBuilder modelBuilder)17 {18 throw new UnintentionalCodeFirstException();19 }20 21 public virtual DbSet<Course> Courses { get; set; }22 public virtual DbSet<Standard> Standards { get; set; }23 public virtual DbSet<Student> Students { get; set; }24 public virtual DbSet<StudentAddress> StudentAddresses { get; set; }25 public virtual DbSet<Teacher> Teachers { get; set; }26 public virtual DbSet<View_StudentCourse> View_StudentCourse { get; set; }27 28 public virtual ObjectResult<GetCoursesByStudentId_Result> GetCoursesByStudentId(Nullable<int> studentId)29 {30 var studentIdParameter = studentId.HasValue ?31 new ObjectParameter("StudentId", studentId) :32 new ObjectParameter("StudentId", typeof(int));33 34 return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetCoursesByStudentId_Result>("GetCoursesByStudentId", studentIdParameter);35 }36 37 public virtual int sp_DeleteStudent(Nullable<int> studentId)38 {39 var studentIdParameter = studentId.HasValue ?40 new ObjectParameter("StudentId", studentId) :41 new ObjectParameter("StudentId", typeof(int));42 43 return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_DeleteStudent", studentIdParameter);44 }45 46 public virtual ObjectResult<Nullable<decimal>> sp_InsertStudentInfo(Nullable<int> standardId, string studentName)47 {48 var standardIdParameter = standardId.HasValue ?49 new ObjectParameter("StandardId", standardId) :50 new ("StandardId", typeof(int));51 52 var studentNameParameter = studentName != null ?53 new ObjectParameter("StudentName", studentName) :54 new ObjectParameter("StudentName", typeof(string));55 56 return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<decimal>>("sp_InsertStudentInfo", standardIdParameter, studentNameParameter);57 }58 59 public virtual int sp_UpdateStudent(Nullable<int> studentId, Nullable<int> standardId, string studentName)60 {61 var studentIdParameter = studentId.HasValue ?62 new ObjectParameter("StudentId", studentId) :63 new ObjectParameter("StudentId", typeof(int));64 65 var standardIdParameter = standardId.HasValue ?66 new ObjectParameter("StandardId", standardId) :67 new ObjectParameter("StandardId", typeof(int));68 69 var studentNameParameter = studentName != null ?70 new ObjectParameter("StudentName", studentName) :71 new ObjectParameter("StudentName", typeof(string));72 73 return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_UpdateStudent", studentIdParameter, standardIdParameter, studentNameParameter);74 }75 }76 }
View Code
The preceding example shows that the context class contains all entity sets of the DbSet <TEntity> type. It also contains the stored procedure and view functions in EDM.
The Context class overrides the OnModelCreating method. The DbModelBuilder parameter provides a stream API to configure the relationship between the real bodies in Code-First.
Instantiate DbContext:
Instantiate DbContext to perform the CRUD operation.
1 2 using (var ctx = new SchoolDBEntities () 3 {4 5 // here perform the CRUD operation .. 6} 7
View Code
Obtain from DbContextObjectContext:
In common tasks, APIs in DBContext are easier to use than those in ObjectContext. Of course, you can also get the reference of ObjectContext from DBContext to use some of these methods. This can be done through IObjectContextAdpter.
1 2 using (var ctx = new SchoolDBEntities () 3 {4 var objectContext = (ctx as System. data. entity. infrastructure. IObjectContextAdapter ). objectContext; 5 6 // use objectContext here .. 7} 8
View Code