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. java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException

    问题描述:启动tomcat服务器的时候,报找不到JoranException类的异常 原因:tomcat中没有logback-core-1.1.2.jar包 解决方法:在tomcat中的lib目录添加 ...

  2. git 修改远程仓库地址

    以前的老项目需要修改git路径,为了保留之前的上传记录和分支等可以通过以下方法解决这个问题 sourceTree项目远程仓库,直接修改origin路径,然后提交一个commit即可将项目上传到新的gi ...

  3. shell常用操作积累

    1. 拼接字符串* #!/bin/sh write_log(){ local up_name=$ local num=${#string} ]; do up_name="$up_name*& ...

  4. mongodb 安装及使用

    https://www.cnblogs.com/shileima/p/7823434.html

  5. div+css 画三角形

            <style type="text/css"> .rightdirection { width:0;height:0; line-height:0; b ...

  6. UFLDL深度学习笔记 (五)自编码线性解码器

    UFLDL深度学习笔记 (五)自编码线性解码器 1. 基本问题 在第一篇 UFLDL深度学习笔记 (一)基本知识与稀疏自编码中讨论了激活函数为\(sigmoid\)函数的系数自编码网络,本文要讨论&q ...

  7. saltstack内置state模块file之managed

    managed管理一个模板文件,载入到各个节点并运行相应配置 salt.states.file.managed(name, source=None, source_hash='', user=None ...

  8. SourceTree超前一个版本,落后N个版本

    SourceTree超前一个版本,落后N个版本 在使用SourceTree的时候经常会遇见超前一个版本,落后N个版本的情况,遇见这种情况应该怎么办呢?   首先打开终端,最好是从SourceTree里 ...

  9. hiho一下 第二周&第四周:从Trie树到Trie图

    hihocoder #1014 题目地址:http://hihocoder.com/problemset/problem/1014 hihocoder #1036 题目地址: http://hihoc ...

  10. vue2 本地安装