EntityFramework 学习 一 DBEntityEntry
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: |
| 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的更多相关文章
- entityframework学习笔记--001
最近想重新好好学习一下entityframework,于是在院子里找到了一篇不错的博客.下面把学习的过程记录下来,方便以后复习. 学习过程参考大神的博客:http://www.cnblogs.com/ ...
- EntityFramework 学习资料
1.EF框架step by step 2.Entity Framework Code First 学习日记 3.[译著]Code First :使用Entity. Framework编程 4.Enti ...
- EntityFramework 学习 一 Querying with EDM 从EDM查询
前面我们已经创建EDM.DbContext和实体类,接下来我们学习不同的查询实体方法,转变为数据库的SQL查询 Entity Framework支持3种查询方式:1)LINQ to Entities ...
- entityframework学习笔记--005-给code first一个正确的解释
在微软官方关于ef7的介绍中强调,ef7将舍弃database first.model first,只保留code first的使用.这引起了很多人的担忧,担忧源自对code first的错误理解.因 ...
- entityframework学习笔记--004-无载荷与有载荷关系
1.无载荷(with NO Payload)的多对多关系建模 在数据库中,存在通过一张链接表来关联两张表的情况.链接表仅包含连接两张表形成多对多关系的外键,你需要把这两张多对多关系的表导入到实体框架模 ...
- EntityFramework学习
本文档主要介绍.NET开发中两项新技术,.NET平台语言中的语言集成查询技术 - LINQ,与ADO.NET中新增的数据访问层设计技术ADO.NET Entity Framework.ADO.NET的 ...
- EntityFramework 学习 一 Validate Entity
可以为实体实现自定义验证,重写DBContext中的个ValidateEntity方法 protected override System.Data.Entity.Validation.DbEntit ...
- EntityFramework 学习 一 并发
EntityFramework默认支持乐观并发 乐观并发中,实体加载后如果都没发生变化,ef保存该实体 首先,我们需要一个rowversion列为了控制student实体的并发问题,rowversio ...
- EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario
using System; using System.Collections.Generic; public partial class Student { public Student() { th ...
随机推荐
- 使用Office 365前,企业必须要知道的10件事
目前的市场上充斥着很多关于微软Office 365的炒作,相信厂商.客户或者企业的都有自己不同的考虑.Office 365是微软云版本的Office,用户可以通过互联网创建一个帐户,付款.下载应用安装 ...
- 排序算法 python
1.先写个原始数组和测试算法是否正确,输出多次,方便计算算法运算的平均值 2.开始第一个最简单的冒泡排序 3.“”选择排序“”,跟冒泡很像,每次选最大/最小,放进新list中. 3.1发现测试test ...
- saltstack内置state模块file之managed
managed管理一个模板文件,载入到各个节点并运行相应配置 salt.states.file.managed(name, source=None, source_hash='', user=None ...
- shiro 实现自己定义权限规则校验
<span style="font-family: Arial, Helvetica, sans-serif;">在系统中使用shiro进行权限管理,当用户訪问没有权限 ...
- Android自定义属性format类型
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...
- 安装Hadoop 1.1.2 (三 安装配置Hadoop)
1 tar -zxvf hadoop-1.1.2.tar.gz 2 在hadoop/conf目录 (1) 编辑 hadoop-env.sh export JAVA_HOME=/usr/java/jdk ...
- 解决QT:forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget" 等莫名奇异错误
今天在进行QT Widget的UI设计时,改了下Widget的对象名,然后在多次成功编译执行后,执行清理,又一次构建,就出现了好多莫名奇异的错误: widget.h:12: 错误:forward de ...
- c++的检测的确比C++更严格
见下面代码 #include <stdio.h> #include <stdlib.h> #include <time.h> enum guess { paper, ...
- hdu 3549 Flow Problem【最大流增广路入门模板题】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...
- effect request
from bs4 import BeautifulSoup import os filepath = 'D:\\pymine\\clean\\spider_map\\baidu_map_html_fi ...