zoukankan      html  css  js  c++  java
  • Entity Framework Tutorial Basics(7):DBContext

    DBContext:

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

    Entity Framework 5.0 Tutorial

    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.

    Entity Framework dbcontext

    DbContext is the primary class that is responsible for interacting with data as object. DbContext is responsible for the following 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.

    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);
            }
        }
    }  

    As you can see in the above example, context class (SchoolDBEntities) includes entity set of type DbSet<TEntity> for all the entities. Learn more about DbSet class here. It also includes functions for the stored procedures and views included in EDM.

    Context class overrides OnModelCreating method. Parameter DbModelBuilder is called Fluent API, which can be used to configure entities in the Code-First approach.

    Instantiating DbContext:

    You can use DbContext by instantiating context class and use for CRUD operation as shown below.

    using (var ctx = new SchoolDBEntities())
    {
            
        //Can perform CRUD operation using ctx here..
    }

    Getting ObjectContext from DbContext:

    DBContext API is easier to use than ObjectContext API for all common tasks. However, you can get the reference of ObjectContext from DBContext in order to use some of the features of ObjectContext. This can be done by using IObjectContextAdpter as shown below:

    using (var ctx = new SchoolDBEntities())
    {
        var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext;
            
        //use objectContext here..
    }

    EDM also generates entity classes. Learn about the different types of entity in the next chapter.

  • 相关阅读:
    UVA307Sticksdfs+剪枝
    UVA 10382 Watering Grass 贪心+区间覆盖问题
    H265网页视频播放器播放H265编码录像视频文件如何减缓卡顿情况?
    RTMP/RTSP/GB28181协议视频平台授权页面配置完成后,控制台出现报错如何解决?
    通过私有化部署自建一套视频流媒体服务器平台,如何解决视频播放延时卡顿问题?
    RTSP/RTMP/HTTP/HLS协议视频流媒体播放器EasyPlayerRTSP安卓版本,切换视频流黑屏怎么办?
    【解决方案】设备众多/品牌不一/协议复杂的需求下如何搭建智慧工地视频集中管控平台,实现建筑工程新形态?
    RTMP/RTSP/GB28181协议视频平台授权页面开发中数字框输入字母BUG解决方法
    国标GB28181流媒体协议EasyGBS视频平台集成第三方平台播放器,显示logo水印如何去除?
    通过私有化部署自建视频流媒体服务器,不同视频流延时说明
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5648903.html
Copyright © 2011-2022 走看看