EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario
- using System;
- using System.Collections.Generic;
- public partial class Student
- {
- public Student()
- {
- this.Courses = new HashSet<Course>();
- }
- public int StudentID { get; set; }
- public string StudentName { get; set; }
- public Nullable<int> StandardId { get; set; }
- public byte[] RowVersion { get; set; }
- public virtual Standard Standard { get; set; }
- public virtual StudentAddress StudentAddress { get; set; }
- public virtual ICollection<Course> Courses { get; set; }
- }
- 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)
- {
- }
- 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; }
- }
- Student stud;
- //1. Get student from DB
- using (var ctx = new SchoolDBEntities())
- {
- stud = ctx.Students.Where(s => s.StudentName == "New Student1").FirstOrDefault<Student>();
- }
- //2. change student name in disconnected mode (out of ctx scope)
- if (stud != null)
- {
- stud.StudentName = "Updated Student1";
- }
- //save modified entity using new Context
- using (var dbCtx = new SchoolDBEntities())
- {
- //3. Mark entity as modified
- dbCtx.Entry(stud).State = System.Data.Entity.EntityState.Modified;
- //4. call SaveChanges
- dbCtx.SaveChanges();
- }
1.从DB中获取存在是student
2.在context作用域之外设置studentname
3.使用Context.Entry()方法获取实体的DBEntityEntry,并更改实体状态为modified
EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario的更多相关文章
- EntityFramework 学习 一 Add New Entity using DBContext in Disconnected Scenario
using System; using System.Collections.Generic; public partial class Student { public Student() { th ...
- EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario
Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...
- 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 ...
- EntityFramework 学习 一 Update Entity Graph using DbContext:
使用主键属性 每个实体必须有主键 默认值的id属性值必须为0 在context2中,它不知道实体的状态, 只能通过实体的主键来判断实体的状态 如果主键为0,则是新的对象,不为0 就是修改 Standa ...
- EntityFramework 学习 一 Persistence in Entity Framework
实体框架的持久化 当用EntityFramework持久化一个对象时,有两种情形:连接的和断开的 1.连接场景:使用同一个context上下文从数据库中查询和持久化实体时,查询和持久化实体期间,con ...
- Entity Framework Tutorial Basics(24):Update Single Entity
Update Existing Entity using DBContext in Disconnected Scenario: In this chapter, you will learn how ...
- EntityFramework.Extended 实现 update count+=1
在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...
- Entity Framework DBContext 增删改查深度解析
Entity Framework DBContext 增删改查深度解析 有一段时间没有更新博客了,赶上今天外面下雨,而且没人约球,打算把最近对Entity Framework DBContext使用的 ...
- 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 ...
随机推荐
- Jquery跨域请求php数据(jsonp)
Jquery跨域请求php数据 我们一般用到ajax的时候是在同服务器下,一般情况下不会跨域,但有时候需要调用其他域名或ip下的数据的时候,遇到跨域请求数据的时候. 今天在工作中碰到javascrip ...
- centos7 firefox 安装flash
在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...
- spring 事件驱动模型简介
事件驱动模型简介 事件驱动模型也就是我们常说的观察者,或者发布-订阅模型:理解它的几个关键点: 首先是一种对象间的一对多的关系:最简单的如交通信号灯,信号灯是目标(一方),行人注视着信号灯(多方): ...
- 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 ...
- [译]GLUT教程 - 键盘高级特性
Lighthouse3d.com >> GLUT Tutorial >> Input >> Advanced Keyboard 本节我们会介绍另外4个处理键盘事件的 ...
- Linux 查看tomcat占用的端口号
第一步:先查看tomcat占用的进程号 ps -ef|grep tomcat 第二步:根据进程号,查看进程所占用的端口 netstat -apn 由此得知,tomcat的进程号是21845,并得到端口 ...
- springmvc上传方法
/** * * @param file 上传的文件 * @param filePath 上传到那个目录 * @return 上传后的文件名字 * @throws IOException */ publ ...
- 利用github Pages和Jekyll搭建blog实践1
你必须要懂一点git和网页开发.安装了git,并且有github账户. github设计了Pages功能,允许用户自定义项目首页 github提供模板,允许站内生成网页,但也允许用户自己编写网页,然后 ...
- Xcode 5、Xcode 6 免证书真机调试
我们都知道,在iOS开发中,假设要进行真机调试的话是须要苹果开发人员账号的.否则Xcode就不可以进行真机调试.仅仅可以在模拟器上执行:这就带来了非常多问题,比方iCloud编程的话你不可以用模拟器. ...
- web安全之SQL注入---第二章 什么是sql注入?
如何理解SQL注入?SQL注入是一种将SQL代码添加到输入参数中传递到SQL服务器解析并执行的一种攻击方法总结:其实就是输入的参数没有进行过滤,直接参加sql语句的运算,达到不可预想的结果.SQL注入 ...