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. VC6常用插件

    VC6常用插件 2009-10-09 17:27 1.Visual Assist(强烈推荐)    http://www.wholetomato.com/     VA从5.0一直到现在的VAX,功能 ...

  2. Java VM(虚拟机) 参数

    -XX:PermSize/-XX:MaxPermSize,永久代内存: 1. 虚拟机参数:-ea,支持 assert 断言关键字 eclipse 默认是不开启此参数的,也就是虽然编译器支持 asser ...

  3. 10个 NPM 使用技巧

    对于一个项目,常用的一些npm简单命令包含的功能有:初始化一个文件夹( npm init ),下载npm模块( npm install ),创建测试( npm tese ) 和自定义脚本( npm r ...

  4. nodejs 设置跨域访问

    app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); a ...

  5. java编程思想第八章多态

    前言: 封装:通过合并特征和行为创建新的数据类型. 实现隐藏:通过将细节“私有化”,把接口和实现分离. 多态:消除类型间的耦合关系.也称作动态绑定,后期绑定或运行时绑定. 8.1再论向上转型: 对象既 ...

  6. Oracle之into

    ), NVL() INTO SALE_ID, STORE_ID FROM SALEFROMSTORE WHERE ORDERID = IN_ORDER_ID; 这里要注意,into的时候是一个sele ...

  7. svn Can't revert without reverting children 解决方案

    EMZ3.0 qrh$ svn commit -m ""svn: E155010: Commit failed (details follow):svn: E155010: '/U ...

  8. 机器学习:逻辑回归(scikit-learn 中的逻辑回归)

    一.基础理解 使用逻辑回归算法训练模型时,为模型引入多项式项,使模型生成不规则的决策边界,对非线性的数据进行分类: 问题:引入多项式项后,模型变的复杂,可能产生过拟合现象: 方案:对模型正则化处理,损 ...

  9. phonegap 解决https访问问题

    ios报错 在AppDelegate.m文件在最后加入(在@end后面加空行): @implementation NSURLRequest(DataController) + (BOOL)allows ...

  10. Drools学习笔记4—Consequence/RHS

    Right Hand Side,当LHS所有条件满足才会执行 可以使用LHS部分定义的绑定变量.全局变量.或者直接编写JAVA代码. 提供宏函数操作working memory fact对象,如ins ...