ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

EntityFramework 学习 一 DbContext

2019-05-14 13:55:43  阅读:248  来源: 互联网

标签:学习 ObjectContext ObjectParameter DbContext EntityFramework var new public


上一节中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..
}
         

复制代码

 

标签:学习,ObjectContext,ObjectParameter,DbContext,EntityFramework,var,new,public
来源: https://blog.csdn.net/u011966339/article/details/90204463

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有