Entity Framework Tutorial Basics(18):DBEntityEntry Class
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);
} }
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: |
| 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的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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 ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 使用NSUserDefaults保存自定义对象(转)
转自http://zani.iteye.com/blog/1431239 .h文件 #import <Foundation/Foundation.h> @interface MyObjec ...
- 分析 PHP升级导致系统负载过高问题(转载)
原文:http://chuansongme.com/n/797172 背景 据XX部门兄弟反应, 其在将PHP从5.3.8 升级到5.5.13 时, 开始运行正常, 运行一段时间后, 系统负载变高,达 ...
- fakeroot: preload library `libfakeroot.so' not found, aborting.
/**************************************************************************** * fakeroot: preload li ...
- loj #6247. 九个太阳
求 $\sum\limits_{i=1}^n [k | i] \times C_n^i$ 膜 $998244353$ $n \leq 10^{15},k \leq 2^{20}$ $k$ 是 $2$ ...
- CODEVS 1174 靶形数独
题目描述 Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地 迷上了数独游戏,好胜的他们想用数独来一比高低.但普通 的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z ...
- 学习动态性能表(21)v$lincense
学习动态性能表 第20篇--v$lincense 2007.6.15 本节主要参数: SESSION_MAX:实例允许的并发最大session数量 SESSION_WARNING:当前实例警告的并发 ...
- QT:QString、QByteArray和char *的转换 【转载】
原文网址:http://blog.csdn.net/light1028/article/details/7899541 第一种,数据流的方式,这里只说从QByteArray转向QString. QBy ...
- Python collections系列之单向队列
单向队列(deque) 单项队列(先进先出 FIFO ) 1.创建单向队列 import queue q = queue.Queue() q.put(') q.put('evescn') 2.查看单向 ...
- Linux系统下如何去掉文件的@属性
前几天从别处copy了一个memcached.so文件,发现运行不了,用ls -l 看了一下发现memcached.so多了一个@属性,如何去掉这个@属性呢? 第一步:ls -laeO@ 第二步:xa ...
- Java学习之SpringMVC零配置实践
概述:本实践主要是对SpringMVC的主要功能做了一个大概的体验,将原来的SpringMVC的大量配置改成用SpringBoot进行集成,做到了零XML配置,本次实践分为两个部分,一部分为基本功能实 ...