1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public partial class Student
  5. {
  6. public Student()
  7. {
  8. this.Courses = new HashSet<Course>();
  9. }
  10.  
  11. public int StudentID { get; set; }
  12. public string StudentName { get; set; }
  13. public Nullable<int> StandardId { get; set; }
  14. public byte[] RowVersion { get; set; }
  15.  
  16. public virtual Standard Standard { get; set; }
  17. public virtual StudentAddress StudentAddress { get; set; }
  18. public virtual ICollection<Course> Courses { get; set; }
  19. }
  1. using System;
  2. using System.Data.Entity;
  3. using System.Data.Entity.Infrastructure;
  4. using System.Data.Entity.Core.Objects;
  5. using System.Linq;
  6.  
  7. public partial class SchoolDBEntities : DbContext
  8. {
  9. public SchoolDBEntities()
  10. : base("name=SchoolDBEntities")
  11. {
  12. }
  13.  
  14. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  15. {
  16.  
  17. }
  18.  
  19. public virtual DbSet<Course> Courses { get; set; }
  20. public virtual DbSet<Standard> Standards { get; set; }
  21. public virtual DbSet<Student> Students { get; set; }
  22. public virtual DbSet<StudentAddress> StudentAddresses { get; set; }
  23. public virtual DbSet<Teacher> Teachers { get; set; }
  24. }
  1. Student stud;
  2. //1. Get student from DB
  3. using (var ctx = new SchoolDBEntities())
  4. {
  5. stud = ctx.Students.Where(s => s.StudentName == "New Student1").FirstOrDefault<Student>();
  6. }
  7.  
  8. //2. change student name in disconnected mode (out of ctx scope)
  9. if (stud != null)
  10. {
  11. stud.StudentName = "Updated Student1";
  12. }
  13.  
  14. //save modified entity using new Context
  15. using (var dbCtx = new SchoolDBEntities())
  16. {
  17. //3. Mark entity as modified
  18. dbCtx.Entry(stud).State = System.Data.Entity.EntityState.Modified;
  19.  
  20. //4. call SaveChanges
  21. dbCtx.SaveChanges();
  22. }

1.从DB中获取存在是student

2.在context作用域之外设置studentname

3.使用Context.Entry()方法获取实体的DBEntityEntry,并更改实体状态为modified

EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario的更多相关文章

  1. EntityFramework 学习 一 Add New Entity using DBContext in Disconnected Scenario

    using System; using System.Collections.Generic; public partial class Student { public Student() { th ...

  2. EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario

    Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...

  3. EntityFramework 学习 一 Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0

    To migrate your existing Entity Framework 4.x project to Entity Framework 5.0 using VS2012, first ta ...

  4. EntityFramework 学习 一 Update Entity Graph using DbContext:

    使用主键属性 每个实体必须有主键 默认值的id属性值必须为0 在context2中,它不知道实体的状态, 只能通过实体的主键来判断实体的状态 如果主键为0,则是新的对象,不为0 就是修改 Standa ...

  5. EntityFramework 学习 一 Persistence in Entity Framework

    实体框架的持久化 当用EntityFramework持久化一个对象时,有两种情形:连接的和断开的 1.连接场景:使用同一个context上下文从数据库中查询和持久化实体时,查询和持久化实体期间,con ...

  6. Entity Framework Tutorial Basics(24):Update Single Entity

    Update Existing Entity using DBContext in Disconnected Scenario: In this chapter, you will learn how ...

  7. EntityFramework.Extended 实现 update count+=1

    在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...

  8. Entity Framework DBContext 增删改查深度解析

    Entity Framework DBContext 增删改查深度解析 有一段时间没有更新博客了,赶上今天外面下雨,而且没人约球,打算把最近对Entity Framework DBContext使用的 ...

  9. DbContextScope,A simple and flexible way to manage your Entity Framework DbContext instances,by mehdime

    DbContextScope A simple and flexible way to manage your Entity Framework DbContext instances. DbCont ...

随机推荐

  1. Jquery跨域请求php数据(jsonp)

    Jquery跨域请求php数据 我们一般用到ajax的时候是在同服务器下,一般情况下不会跨域,但有时候需要调用其他域名或ip下的数据的时候,遇到跨域请求数据的时候. 今天在工作中碰到javascrip ...

  2. centos7 firefox 安装flash

    在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...

  3. spring 事件驱动模型简介

    事件驱动模型简介 事件驱动模型也就是我们常说的观察者,或者发布-订阅模型:理解它的几个关键点: 首先是一种对象间的一对多的关系:最简单的如交通信号灯,信号灯是目标(一方),行人注视着信号灯(多方): ...

  4. 50 years of Computer Architecture: From the Mainframe CPU to the Domain-Specific TPU and the Open RISC-V Instruction Set

    1.1960年代(大型机) IBM发明了具有二进制兼容性的ISA——System/360,可以兼容一系列的8到64位的硬件产品,而不必更换操作系统.这是通过微编程实现的,每个计算机模型都有各自的ISA ...

  5. [译]GLUT教程 - 键盘高级特性

    Lighthouse3d.com >> GLUT Tutorial >> Input >> Advanced Keyboard 本节我们会介绍另外4个处理键盘事件的 ...

  6. Linux 查看tomcat占用的端口号

    第一步:先查看tomcat占用的进程号 ps -ef|grep tomcat 第二步:根据进程号,查看进程所占用的端口 netstat -apn 由此得知,tomcat的进程号是21845,并得到端口 ...

  7. springmvc上传方法

    /** * * @param file 上传的文件 * @param filePath 上传到那个目录 * @return 上传后的文件名字 * @throws IOException */ publ ...

  8. 利用github Pages和Jekyll搭建blog实践1

    你必须要懂一点git和网页开发.安装了git,并且有github账户. github设计了Pages功能,允许用户自定义项目首页 github提供模板,允许站内生成网页,但也允许用户自己编写网页,然后 ...

  9. Xcode 5、Xcode 6 免证书真机调试

    我们都知道,在iOS开发中,假设要进行真机调试的话是须要苹果开发人员账号的.否则Xcode就不可以进行真机调试.仅仅可以在模拟器上执行:这就带来了非常多问题,比方iCloud编程的话你不可以用模拟器. ...

  10. web安全之SQL注入---第二章 什么是sql注入?

    如何理解SQL注入?SQL注入是一种将SQL代码添加到输入参数中传递到SQL服务器解析并执行的一种攻击方法总结:其实就是输入的参数没有进行过滤,直接参加sql语句的运算,达到不可预想的结果.SQL注入 ...