zoukankan      html  css  js  c++  java
  • Entity Framework 教程——DBContext

    DBContext:

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

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

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

    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数据库创建的

     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 : DbContext
    10     {
    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

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

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

    实例化 DbContext:

    实例化DbContext 来执行CRUD操作。

    1             
    2 using (var ctx = new SchoolDBEntities())
    3 {
    4         
    5     //在这里执行CRUD操作..
    6 }
    7      
    View Code

    从DbContext 中获取ObjectContext :

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

    1            
    2 using (var ctx = new SchoolDBEntities())
    3 {
    4     var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext;
    5         
    6     //在这里使用objectContext..
    7 }
    8       
    View Code
  • 相关阅读:
    超经典~超全的jQuery插件大全
    如何用PHP做到页面注册审核
    php实现签到功能
    php中的实用分页类
    微信小程序,超能装的实例教程
    php之 常用的 流程管理
    php之 人员的权限管理(RBAC)
    php之简单的文件管理(基本功能)
    php最新学习-----文件的操作
    关于LAMP的配置之(虚拟机的安装、创建、配置)
  • 原文地址:https://www.cnblogs.com/Inspire-Yi/p/6230567.html
Copyright © 2011-2022 走看看