EntityFramework 学习 一 DbContext
上一节中EDM自动生成SchoolEntities类,该类继承DbContext

EntityFramework4.1之前的版本,EDM生成的类继承ObjectContext,使用ObjectContext稍微有点棘手,DbContext概念上与ObjectContext相似,它是ObjectContext的封装,DbContext是EF重要的组成部分,它是领域或实体类和数据库的桥梁

DbContext是主要的类负责数据和对象互相转化
EntitySet: DbContext包含实体集合(DBSet<TEntity>),所有与数据库表对应的实体
Querying: DbContext将LINQ-to-Entity查询语言转化成SQL查询语言,并发送给数据库
Change Tracking: 当对象已经从数据库中查询到后,DbContext跟踪实体的变化情况
Persisting Data: DbContext执行插入、更新、删除操作
Caching: DbContext默认使用一级缓存,在一个context对象周期内,它缓存所有已经被查询的实体
Manage Relationship: DbContext管理对象间的关系
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);
}
}
}
DbContext实例化,用来执行GRUD操作
using (var ctx = new SchoolDBEntities())
{ //Can perform CRUD operation using ctx here..
}
从DbContext中获取ObjectContext
DbContext API比ObjectContext用起来简单些,然而,你可以从DbContext中得到ObjectContext的引用,
using (var ctx = new SchoolDBEntities())
{
var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext; //use objectContext here..
}
EntityFramework 学习 一 DbContext的更多相关文章
- EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario
using System; using System.Collections.Generic; public partial class Student { public Student() { th ...
- EntityFramework 学习 一 Add New Entity using DBContext in Disconnected Scenario
using System; using System.Collections.Generic; public partial class Student { public Student() { th ...
- EntityFramework 学习 一 Explicit Loading with DBContext
即使延迟加载不能使用,也可以通过明确的调用来延迟加载相关实体 使用DBEntryEntity来完成 using (var context = new SchoolDBEntities()) { //D ...
- EntityFramework 学习 一 Update Entity Graph using DbContext:
使用主键属性 每个实体必须有主键 默认值的id属性值必须为0 在context2中,它不知道实体的状态, 只能通过实体的主键来判断实体的状态 如果主键为0,则是新的对象,不为0 就是修改 Standa ...
- EntityFramework 学习 一 Add Entity Graph using DbContext:
//Create student in disconnected mode Student newStudent = new Student() { StudentName = "New S ...
- EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario
Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...
- EntityFramework 学习 一 Querying with EDM 从EDM查询
前面我们已经创建EDM.DbContext和实体类,接下来我们学习不同的查询实体方法,转变为数据库的SQL查询 Entity Framework支持3种查询方式:1)LINQ to Entities ...
- entityframework学习笔记--005-给code first一个正确的解释
在微软官方关于ef7的介绍中强调,ef7将舍弃database first.model first,只保留code first的使用.这引起了很多人的担忧,担忧源自对code first的错误理解.因 ...
- entityframework学习笔记--001
最近想重新好好学习一下entityframework,于是在院子里找到了一篇不错的博客.下面把学习的过程记录下来,方便以后复习. 学习过程参考大神的博客:http://www.cnblogs.com/ ...
随机推荐
- C语言小板凳(1)
①strlen()函数作用:计算字符串的长度,当遇到"\n"字符时结束,即遇到数值"0"时结束计算,有一点特别要注意当这个函数用来计算数组的长度的时候遇到数值0 ...
- 篇章一:[AngularJS] 使用AngularAMD動態載入Controller
前言 使用AngularJS來開發Single Page Application(SPA)的時候,可以選用AngularUI Router來提供頁面內容切換的功能.但是在UI Router的使用情景裡 ...
- MIC中函数和变量的声明
c++/c使用 __declspec(target(mic))函数或变量声明 或 __attribute__((target(mic)))函数或变量声明 举例如下: __attribute__((ta ...
- 【ubantu】Ubuntu的一些常用命令
创建文件: touch a.txt 创建文件夹: mkdir NewFolder 删除文件: rm a.txt 删除文件夹: rmdir NewFolder 删除带有文件的文件夹: rm -rf Ne ...
- linux vmware提示:此虚拟机似乎正在使用中,取得该虚拟机的所有权失败错误
用vm的时候,没有挂起和关闭虚拟机,直接关实体机.然后不幸的就异常了. 然后今天用的时候提示 此虚拟机似乎正在使用中. 如果此虚拟机已在使用中,请按“取消”按钮,以免损坏它.如果此虚拟机未使用,请按“ ...
- python之开篇---hello world!
(1)前沿 (2)python 简介 (3)python hello world 实现 (4) -------------qq:1327706646 ------------------------- ...
- SQL_为表和列加凝视
***********************************************声明*************************************************** ...
- 小技巧:怎样以另外的Windows用户执行SSMS
可能会碰到这种问题.你须要在一台机器上面使用不同的Windows账户连接到SQL Server做測试.默认情况下,你须要用不同的Windows账户登录然后測试. 实际上不须要每一个windows登陆. ...
- usermod命令
usermod 功能: 修改用户 常用参数:-c 账号说明-d 账号家目录-e 密码失效日期-g 主用户组GID-G 次用户组GID-l 账号名称-s she ...
- 在ListView的GroupItem头中显示每列的Summary
问题描述 WPF自带的ListView和DataGrid控,都提供了数据分组的支持,并可以对分组的Header进行自定义.但是,如果想在每个分组的Header中,显示出本分组的"小计&quo ...