美文网首页
(转)Entity Framework 教程——DBContex

(转)Entity Framework 教程——DBContex

作者: aslbutton | 来源:发表于2019-03-11 14:32 被阅读0次

    DBContext:

    在之前的章节《创建实体数据模型》中,EDM为我们创建了SchoolDBEntities 类,它派生子System.Data.Entity.DbContext这个类,这个DbContext在EF中被称作上下文类。

    image
    在EF4.1之前,EDM生成的上下文类是派生自ObjectContext这个类的。它有点难于使用。DbContext 在概念上类似于ObjectContext。DbContext 只是对ObjectContext 进行了封装使其更容易在所有开发场景中使用。(如Code First, Model First 和 Database First 中)

    DbContext 是EF中重要的一环,它是数据库与你应用程序域或实体类的桥梁。


    image

    DbContext 是负责数据与对象互操作的主要的类型。它主要负责以下一些动作:

    EntitySet : DbContext 包含所有从数据库表中被映射出来的实体对象的集合(如DbSet<TEntity>)。

    Querying : DbContext 将LINQ To Entities 转化为SQL 查询语句并发送至数据库。

    Change Tracking : 它保持变更追踪,一旦实体对象发生改变它就会从数据库中进行查询。

    Persisting Data : 它也可以基于实体状态对数据库进行插入,更新和删除操作。

    Caching : DbContext 默认作一级缓存,它存储在上下文类的生命周期中检索过的实体对象。

    Manage Relationship : 在DB-First 或 Model-First 中 DbContext 使用CSDL, MSL 和 SSDL 管理关系,在Code-First中使用流式API管理关系。

    Object Materialization : DbContext 将原始的表数据转化至实体对象中。

    以下例子中的SchoolDBEntities 类是又EDM根据SchoolDB数据库创建的

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

    你可以通过上面的例子看出context 类包含类型为DbSet<TEntity>的所有实体集合。也包含EDM中存储过程及视图所对应的函数。

    Context 类重写了 OnModelCreating 方法,参数DbModelBuilder 提供流式API来配置Code-First中实体的关系。

    实例化 DbContext:

    实例化DbContext 来执行CRUD操作。

    using (var ctx = new SchoolDBEntities())
    {
            
        //在这里执行CRUD操作..
    }
    

    从DbContext 中获取ObjectContext :

    在常见的任务中DBContext 中的API 相较于ObjectContext 的API 而言更加容易使用。当然你也可以从DBContext 中获取ObjectContext 的引用来使用其中的一些方法。可以同过IObjectContextAdpter 来完成。

    using (var ctx = new SchoolDBEntities())
    {
        var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext;
            
        //在这里使用objectContext..
    }
    

    相关文章

      网友评论

          本文标题:(转)Entity Framework 教程——DBContex

          本文链接:https://www.haomeiwen.com/subject/klfbpqtx.html