DBEntityEntry Class

DBEntityEntry is an important class, which is useful in retrieving various information about an entity. You can get an instance of DBEntityEntry of a particular entity by using Entry method of DBContext. For example:

DBEntityEntry studentEntry = dbcontext.Entry(StudentEntity);

DBEntityEntry enables you to access entity state, current, and original values of all the property of a given entity. The following example code shows how to retrieve important information of a particular entity.

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);
} }
Output:

Entity Name: Student 
Entity State: Modified 
********Property Values********
Property Name: StudentID
Original Value: 1
Current Value: 1
Property Name: StudentName 
Original Value: First Student Name 
Current Value: Edited name 
Property Name: StandardId
Original Value:
Current Value:

DbEntityEntry enables you to set Added, Modified or Deleted EntityState to an entity as shown below.

context.Entry(student).State = System.Data.Entity.EntityState.Modified;

DBEntityEntry class has the following important methods:

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();

Visit MSND for more information on DBEntityEntry class.

Entity Framework Tutorial Basics(18):DBEntityEntry Class的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  2. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  3. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

  4. Entity Framework Tutorial Basics(42):Colored Entity

    Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...

  5. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  6. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

  7. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  8. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

  9. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

随机推荐

  1. /dev/root: No such file or directory

    /*************************************************************************** * /dev/root: No such fi ...

  2. Python学习笔记一(基础信息)

    目录 输入输出 数据类型和变量 整数 浮点数 字符串 布尔值 空值 变量 常量 小结 欢迎关注我的博客我在马路边 说明:此笔记不是从零开始,在学习的过程中感觉需要记录一些比较重要和需要重复浏览的信息, ...

  3. 大整数乘法(Comba 乘法 (Comba  Multiplication)原理)

    Comba 乘法以(在密码学方面)不太出名的 Paul G. Comba 得名.上面的笔算乘法,虽然比较简单, 但是有个很大的问题:在 O(n^2) 的复杂度上进行计算和向上传递进位,看看前面的那个竖 ...

  4. JSONP原理及实现跨域方式

    今天做页面时,后台给了个接口:https://a.a.com/a/a.json,我页面的上线地址是:http://b.b.com.显而易见,因为浏览器同源策略的限制,通过ajax无法无法取得json的 ...

  5. webpack 插件

    插件可以完成更多 loader 不能完成的功能. 插件的使用一般是在 webpack 的配置信息 plugins 选项中指定. Webpack 本身内置了一些常用的插件,还可以通过 npm 安装第三方 ...

  6. FastAdmin 的 Bootstrap-Table 如何合并字段?

    FastAdmin 的 Bootstrap-Table 如何合并字段? ★hey-成都 14:13:34 把下面那个字段合并到上面那个字段是用什么方法 ^★暗物质-江西 14:17:21 city加上 ...

  7. [转]angular之$apply()方法

    这几天,根据buddy指定的任务,要分享一点angular JS的东西.对于一个在前端属于纯新手的我来说,Javascript都还是一知半解,要想直接上手angular JS,遇到的阻力还真是不少.不 ...

  8. Centos下安装禅道

    1.下载禅道安装包:http://dl.cnezsoft.com/zentao/9.7/ZenTaoPMS.9.7.stable.zbox_64.tar.gz 2.将下载的压缩包解压到/opt目录下: ...

  9. python sentence

    1.while for 增加了循环正常结束后执行的else代码块. 2.Objects are mutable 3.import copy p1 = Point() p2=copy.copy(p1) ...

  10. HTML5两个打包工具

    AppCan:http://www.appcan.cn/ HBulider:http://www.dcloud.io/