DbEntityEntry是一个重要的类,用来获取各种各样的实体信息

可以通过DBContext的Entry方法获取DbEntityEntry的实例

DBEntityEntry studentEntry = dbcontext.Entry(StudentEntity);

通过DBEntityEntry,可以获取实体的状态,所有属性的当前值和原始值

using (var dbCtx = new SchoolDBEntities())
{
//get student whose StudentId is 1
var student = dbCtx.Students.Find(); //edit student name
student.StudentName = "Edited name"; //get DbEntityEntry object for student entity object
var entry = dbCtx.Entry(student); //get entity information e.g. full name
Console.WriteLine("Entity Name: {0}", entry.Entity.GetType().FullName); //get current EntityState
Console.WriteLine("Entity State: {0}", entry.State ); Console.WriteLine("********Property Values********"); foreach (var propertyName in entry.CurrentValues.PropertyNames )
{
Console.WriteLine("Property Name: {0}", propertyName); //get original value
var orgVal = entry.OriginalValues[propertyName];
Console.WriteLine(" Original Value: {0}", orgVal); //get current values
var curVal = entry.CurrentValues[propertyName];
Console.WriteLine(" Current Value: {0}", curVal);
} }

DbEntityEntry可以设置实体的状态如Added、Modified或Deleted

context.Entry(student).State = System.Data.Entity.EntityState.Modified;
Method Name Return Type Description
Collection DBCollectionEntry Gets an object that represents the collection navigation property from this entity to a collection of related entities.

Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
var collectionProperty = studentDBEntityEntry.Collection<course>(s => s.Courses);

ComplexProperty DBComplexPropertyEntry Gets an object that represents a complex property of this entity. 
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
var complexProperty = studentDBEntityEntry.ComplexProperty(stud.StudentStandard);
GetDatabaseValues DBPropertyValues Queries the database for copies of the values of the tracked entity as they currently exist in the database. Changing the values in the returned dictionary will not update the values in the database. If the entity is not found in the database then null is returned.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity);
var dbPropValues = studentDBEntityEntry.GetDatabaseValues();
Property DBPropertyEntry Gets an object that represents a scalar or complex property of this entity.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
string propertyName = studentDBEntityEntry.Property("StudentName").Name;
Reference DBReferenceEntry Gets an object that represents the reference (i.e. non-collection) navigation property from this entity to another entity.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
var referenceProperty = studentDBEntityEntry.Reference(s => s.Standard);
Reload void Reloads the entity from the database overwriting any property values with values from the database. The entity will be in the Unchanged state after calling this method.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity);
studentDBEntityEntry.Reload();

EntityFramework 学习 一 DBEntityEntry的更多相关文章

  1. entityframework学习笔记--001

    最近想重新好好学习一下entityframework,于是在院子里找到了一篇不错的博客.下面把学习的过程记录下来,方便以后复习. 学习过程参考大神的博客:http://www.cnblogs.com/ ...

  2. EntityFramework 学习资料

    1.EF框架step by step 2.Entity Framework Code First 学习日记 3.[译著]Code First :使用Entity. Framework编程 4.Enti ...

  3. EntityFramework 学习 一 Querying with EDM 从EDM查询

    前面我们已经创建EDM.DbContext和实体类,接下来我们学习不同的查询实体方法,转变为数据库的SQL查询 Entity Framework支持3种查询方式:1)LINQ to Entities ...

  4. entityframework学习笔记--005-给code first一个正确的解释

    在微软官方关于ef7的介绍中强调,ef7将舍弃database first.model first,只保留code first的使用.这引起了很多人的担忧,担忧源自对code first的错误理解.因 ...

  5. entityframework学习笔记--004-无载荷与有载荷关系

    1.无载荷(with NO Payload)的多对多关系建模 在数据库中,存在通过一张链接表来关联两张表的情况.链接表仅包含连接两张表形成多对多关系的外键,你需要把这两张多对多关系的表导入到实体框架模 ...

  6. EntityFramework学习

    本文档主要介绍.NET开发中两项新技术,.NET平台语言中的语言集成查询技术 - LINQ,与ADO.NET中新增的数据访问层设计技术ADO.NET Entity Framework.ADO.NET的 ...

  7. EntityFramework 学习 一 Validate Entity

    可以为实体实现自定义验证,重写DBContext中的个ValidateEntity方法 protected override System.Data.Entity.Validation.DbEntit ...

  8. EntityFramework 学习 一 并发

    EntityFramework默认支持乐观并发 乐观并发中,实体加载后如果都没发生变化,ef保存该实体 首先,我们需要一个rowversion列为了控制student实体的并发问题,rowversio ...

  9. EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario

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

随机推荐

  1. cocos2dx3.0戳青蛙游戏(打地鼠)

    1戳青蛙项目描写叙述 1.1功能描写叙述 实现类似打地鼠游戏.青蛙随机出如今屏幕左边5*3的格子中,并会向屏幕右边移动,在青蛙逃离之前,手指点击实现戳灭青蛙的效果.随着分数添加,青蛙越来越多,当青蛙逃 ...

  2. executable null\bin\winutils.exe in the Hadoop binaries.

    在windows 使用eclipse远程调用hadoop集群时抛出下面异常 executable null\bin\winutils.exe in the Hadoop binaries. 这个问题 ...

  3. Android Camera子系统之源码View

    本文基于Android 4.2.2+Linux3.6.9+SAMA5D3 SoC从源码的角度审视Android Camera子系统. 应用层 Androd原生Camera应用 /system/app/ ...

  4. 为什么jdbc中的resultset只能取一次去第二次就报错了

    引用 6 楼  的回复: 好吧各位,应该写getInt(1)但是我要问的是为什么第二次用会出错 啊,好古老的一个问题... 第二次出错的原因,是因为你创建Statement的时候,没有指定结果集(游标 ...

  5. 尽管是一个CS专业的学生,小B的数学基础很好并对数值计算有着特别的兴趣,喜欢用计算机程序来解决数学问题。现在,她正在玩一个数值变换的游戏。她发现计算机中经常用不同的进制表示同一个数,如十进制数123表达为16进制时只包含两位数7、11(B),用八进制表示时为三位数1、7、3。按不同进制表达时,各个位数的和也不同,如上述例子中十六进制和八进制中各位数的和分别是18和11。

    include "stdafx.h" #include<iostream> #include<vector> #include <algorithm& ...

  6. 文件大小转换(b,kb,M,GB/TB)

    //转换单位 setupSize(1111111111111); function setupSize($fileSize) { $size = sprintf("%u", $fi ...

  7. 使用java命令运行class文件带包名时出错

    会出现classnotfound的错误. 如:文件x:/Test.java; package xx public class Test{} javac 编译后,java命令出错,需要手动构建包目录xx ...

  8. 7月份计划-----dream

    梦想还是要有的,万一实现了呢? 数学 150[total] 专业课 150[total] 英语 100[total] 政治 100[total] 第一轮复习计划开始执行 1.专业课: 通过课件把所有的 ...

  9. web.xml配置整理

    虽然是做web开发,但是web中的很多配置有的时候却不是很清楚,只是知道怎么配置,于是就把在网上看到各种关于web.xml的东西整理一下: web.xml中url-pattern的3种写法 1完全匹配 ...

  10. City Game(最大子矩阵)

    Bob is a strategy game programming specialist. In his new city building game the gaming environment ...