当前位置:Gxlcms > 数据库问题 > EntityFramework 学习 一 DbContext

EntityFramework 学习 一 DbContext

时间:2021-07-01 10:21:17 帮助过:24人阅读

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

标签:ctc   images   ret   默认   sys   logs   缓存   组成   sql   

人气教程排行