Entity Framework Tutorial Basics(18):DBEntityEntry Class
2016-07-07 10:09
435 查看
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(1); //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(); |
相关文章推荐
- nginx+tomcat7 DOCKER镜像的dockerfile
- hdu 5215(边-双连通分量)
- apache的下载和安装
- Jquery中获取当前页面某iFrame内部元素的方法
- Nginx的http配置结构体的组织结构
- How web servers work
- ACM专题四总结
- [置顶] AndroidStudio中使用Git-高级篇(二)——新建分支和拉取请求(by 星空武哥)
- 恨一个人恨她在的城市,爱一个人爱她在的世界。
- Node.js des加密
- Entity Framework Tutorial Basics(17):DBSet Class
- 如何计算递归算法的时间复杂度
- 如何把EditText的光标隐藏
- IE 11 访问Cordys BOP 4.3 问题分析
- 上传本地项目到github
- vs2013下载地址以及安装方法
- Cisco路由器ACL安全应用—基于时间的ACL
- PHP使用mysql与mysqli连接Mysql数据库用法示例
- Android程序实现自动更新——Android模块化编程
- db2的常见操作