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 ...
随机推荐
- hibernate之6.one2many单向
表结构: 实体类图: CRUD: Student: package com.demo.model; import java.io.UnsupportedEncodingException; impor ...
- 模拟和数字低通滤波器的MATLAB实现
低通滤波器参数:Fs=8000,fp=2500,fs=3500,Rp=1dB,As=30dB,其他滤波器可以通过与低通之间的映射关系实现. %%模拟滤波器 %巴特沃斯——滤波器设计 wp=2*pi*2 ...
- qtav----ffmeg在ubuntu和win10上的编译和运行
最近在windows上和ubuntu上都安装了qtav并且通过了编译测试,实测播放中英文的视频文件功能正常,有图像有声音. 大致情况是,操作系统ubuntu: wkr@sea-X550JK:~$ ca ...
- 详细的obs操作教程(windows/mac)图解
直播推流软件obs操作配置教程包含支持mac和windows配置 1 2 3 4 5 6 7 8 9
- android:分享 一个非常强大的LOG开关---Log.isLoggable
1.API亮点: 此API能够实现不更换APK.在出问题的手机上就直接能抓到有效log,能提升不少工作效率. .API介绍 近期在解决短信问题时.看到一个非常强大的LOG开关---Log.isLogg ...
- PHPMailer 使用 中文乱码
WordPress在用. You'll find plenty more to play with in the examples folder. 中文乱码问题: $mail->CharSet ...
- 快速用CMD打开当前目录
按住shift,鼠标右键,选择在此处打开命令行窗口.
- Eclipse 查看第三方jar包文件源代码解决方法
1.打开第三方依赖包,源文件的快捷键:ctrl + mouseClick 2.由于我们下载的第三方jar 包,如Spring等相关的依赖包时,并没有附加下载相应的源文件,所以经常出现如图的这种问题. ...
- HealthKit详解
1. 导入HealthKit框架 #import <HealthKit/HealthKit.h> 2. 判断设备是否支持HealthKit HealthKit是iOS8加入的API Hea ...
- js绑定键盘enter事件
js写法: document.onkeydown = function(event){ var event=document.all?window.event:event; if((event.key ...