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 ...
随机推荐
- maven初始搭建一个基础项目(spring mvc+spring+jdbc mysql+jstl)
技术选型: 一.项目搭建: 1)创建maven项目 (我博客里面有介绍) 选择aptach的maven-archetype-webapp 填入groupIDhe artifactId等 确认项目名称 ...
- java.lang.UnsupportedOperationException解决方法!!!
在项目中对List进行操作时报错java.lang.UnsupportedOperationException,后来发现操作的List是由数组转换而成的,通过看源码发现问题,并写测试程序如下. 代码块 ...
- C语言小板凳(1)
①strlen()函数作用:计算字符串的长度,当遇到"\n"字符时结束,即遇到数值"0"时结束计算,有一点特别要注意当这个函数用来计算数组的长度的时候遇到数值0 ...
- Android实践--监測网络状态
Android 监測网络状态 我们在使用Android手机时候,一些APP须要网络环境才干执行,所以手机须要可用的网络,无论是2G.3G或者WIFI.甚至有一些比較耗流量的APP仅仅能在WI ...
- 查看Tomcat状态页,管理app,主机管理
Server Status 通过点击这个按钮我们可以查看Tomcat的状态,点击,弹出403错误页面 通过查看官方文档 因此,创建manager.xml # cat /usr/local/tomcat ...
- Angular Material表单提交及验证
AngularJS中一些表单验证属性: 修改过的表单,只要用户修改过表单,无论输入是否通过验证,该值都将返回false{formName}.{inputFieldName}.$dirty 合法的表单, ...
- NGINX不允许向静态文件提交POST方式的请求,否则报405错误(apache中没有出现)
telnet *.*.*.* 80POST /map/navigation/2011winter/jsn/jsn_20120723_pack/pvf.jsnHTTP/1.1Host:*.*.*.* ( ...
- NFS网络文件系统服务(配置实战)
NFS网络文件系统服务(实战) NFS(Network File System)即网络文件系统,它允许网络中的计算机之间通过TCP/IP网络共享资源.让不同的主机系统(NFS的客户端)可以透明地读写位 ...
- window安装redis
1.redis简介redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
- 06 Memcached中的一些参数限制
一: Memcached中的一些参数限制 Key的长度:250字节(二进制协议支持65536个字节) value的限制:1M ,一般都是存储一些文本,如新闻标题等等这个值足够了, 内存的限制:32位下 ...