你的位置:首页 > 软件开发 > ASP.net > 数据上下文【 DnContext】【EF基础系列7】

数据上下文【 DnContext】【EF基础系列7】

发布时间:2016-05-13 12:00:14
DBContext:As you have seen in the previous Create Entity Data Model section, EDM generates the SchoolDBEntities class, which was derived ...

DBContext:

As you have seen in the previous Create Entity Data Model section, EDM generates the SchoolDBEntities class, which was derived from theSystem.Data.Entity.DbContext class, as shown below. The class that derives DbContext is called context class in entity framework.

数据上下文【 DnContext】【EF基础系列7】

 

 

Prior to EntityFramework 4.1, EDM used to generate context classes that were derived from the ObjectContext class. It was a little tricky to work with ObjectContext. DbContext is conceptually similar to ObjectContext. It is a wrapper around ObjectContext which is useful in all the development models: Code First, Model First and Database First.

DbContext is an important part of Entity Framework. It is a bridge between your domain or entity classes and the database.

数据上下文【 DnContext】【EF基础系列7】

 

 

DbContext is the primary class that is responsible for interacting with data as object. DbContext is responsible for the folloget='_blank'>wing activities:

  • EntitySet: DbContext contains entity set (DbSet<TEntity>) for all the entities which is mapped to DB tables.
  • Querying: DbContext converts LINQ-to-Entities queries to SQL query and send it to the database.
  • Change Tracking: It keeps track of changes that occurred in the entities after it has been querying from the database.
  • Persisting Data: It also performs the Insert, Update and Delete operations to the database, based on what the entity states.
  • Caching: DbContext does first level caching by default. It stores the entities which have been retrieved during the life time of a context class.
  • Manage Relationship: DbContext also manages relationship using CSDL, MSL and SSDL in DB-First or Model-First approach or using fluent API in Code-First approach.
  • Object Materialization: DbContext converts raw table data into entity objects.

The following is an example of SchoolDBEntities class (context class that derives DbContext) generated with EDM for SchoolDB database in the previous section.

 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 }        

原标题:数据上下文【 DnContext】【EF基础系列7】

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录