EF中的实体类型【Types of Entity in Entity】(EF基础系列篇8)
We created EDM for existing database in the previous section. As you have learned in the previous section that EDM contains entities for each table in the database. There are two types of Entities in Entity Framework 5.0/6.0: POCO entity and dynamic proxy entity.
EF中有两种类型的实体:POCO Entity和dynamic proxy entity。
POCO Entity (Plain Old CLR Object)
POCO class is the class that doesn't depend on any framework specific base class. It is like any other normal .net class which is why it is called "Plain Old CLR Objects".
These POCO entities (also known as persistence-ignorant objects) support most of the same query, insert, update, and delete behaviors as entity types that are generated by the Entity Data Model. The following is an example of Student POCO entity.
POCO class是一个类,它不依赖任何.NET framework的基类,它就像任何其他的普通类一样,这也是为什么被称之为“Plain Old CLR Object”的原因;
这些由实体数据模型生成的POCO实体支持大多数的增删查改的功能。下面是一个Studnet POCO实体:
public class Student
{
public Student()
{
this.Courses = new List<Course>();
}
public int StudentID { get; set; }
public string StudentName { get; set; }
public Nullable<int> StandardId { get; set; }
public Standard Standard { get; set; }
public StudentAddress StudentAddress { get; set; }
public IList<Course> Courses { get; set; }
}
Dynamic Proxy (POCO Proxy):
Dynamic Proxy is a runtime proxy class of POCO entity. It is like a wrapper class of POCO entity. Dynamic proxy entities allow lazy loading andautomatic change tracking.
POCO entity should meet the following requirements to become a POCO proxy:
- A POCO class must be declared with public access.
- A POCO class must not be sealed (NotInheritable in Visual Basic)
- A POCO class must not be abstract (MustInherit in Visual Basic).
- Each navigation property must be declared as public, virtual
- Each collection property must be ICollection<T>
- ProxyCreationEnabled option must NOT be false (default is true) in context class
The following Student POCO entity meets all of the above requirement to become dynamic proxy entity at runtime.
动态代理是POCO实体运行状态下的代理类,它就像POCO实体的包装类,动态包装实体支持懒加载,自动跟踪改变的特性;
一个POCO实体需要满足下面的条件,才可以成为POCO代理
1.必须定义成Public
2.不能是密封的
3.不能是抽象的;
4.导航属性必须定义成public virtual;
5.每个集合属性必须是Icollection<T>;
6.在context类中,ProxyCreationEnabled 这个选项必须不能是false(默认是true);
下面的POCO Student实体满足了上面的条件,它将会在运行的时候成为动态代理实体;
public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
}
public int StudentID { get; set; }
public string StudentName { get; set; }
public Nullable<int> StandardId { get; set; }
public virtual Standard Standard { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
Note: By default dynamic proxy is enabled for every entity. However, you can disable dynamic proxy by setting the ProxyCreationEnabled option to false in context class.
context.Configuration.ProxyCreationEnabled = false;
EDM generates POCO entities which satisfy the above requirements for a dynamic proxy by default.
At runtime, type of Student will be System.Data.Entity.DynamicProxies.Student as below:
注意:动态代理默认是对每个实体都是有效的,然而你可以手动禁止动态代理,通过在上下文类中设置ProxyCreationEnabled 这个选项为false,而达到目的;
context.Configuration.ProxyCreationEnabled=false;
在运行的时候Student POCO实体就会变成代理类:

代理类有两个属性,一个标量属性,一个是导航属性,
标量属性实际值包含在实体中,例如Student实体有标量属性StudentID和StudentName,这和数据库中字段一致;
导航属性指向另外相关联的实体,Student实体有一个Standard属性作为导航属性,可以保证程序从一个Student导航到相关联的standard实体。
Getting the actual entity type from a dynamic proxy:
You can use ObjectContext.GetObjectType() to find the actual type of dynamic proxy as shown below:
Entity can have two types of properties, Scalar and Navigation properties.
Scalar properties:
Scalar properties are properties whose actual values are contained in the entity. For example, Student entity has scalar properties like StudentId and StudentName. These correspond with the Student table columns.
Navigation properties:
Navigation properties are pointers to other related entities. The Student has Standard property as a navigation property that will enable the application to navigate from a Student to related Standard entity.
EF中的实体类型【Types of Entity in Entity】(EF基础系列篇8)的更多相关文章
- 7.翻译:EF基础系列---EF中的实体类型
原文地址:http://www.entityframeworktutorial.net/Types-of-Entities.aspx 在Entity Framework中有两种实体类型:一种是POCO ...
- Entity Framework 教程——Entity Framework中的实体类型
Entity Framework中的实体类型 : 在之前的章节中我们介绍过从已有的数据库中创建EDM,它包含数据库中每个表所对应的实体.在EF 5.0/6.0中,存在POCO 实体和动态代理实体两种. ...
- 6.翻译:EF基础系列---什么是EF中的实体?
原文地址:http://www.entityframeworktutorial.net/basics/what-is-entity-in-entityframework.aspx EF中的实体就是继承 ...
- EF4.1: Add/Attach and Entity States(EF中的实体状态转换说明)
实体的状态,连接以及 SaveChanges 方法 数据库上下文对象维护内存中的对象与数据库中数据行之间的同步.这些信息在调用 SaveChanges方法被调用的时候使用.例如,当使用 Add 方法传 ...
- .net Core EF统一配置实体类型
一般情况需要对某个实体进行一些配置时代码如下: protected override void OnModelCreating(ModelBuilder modelBuilder) { base.On ...
- ADO.NET EF 中的实体修改方法
http://www.cnblogs.com/zfz15011/archive/2010/05/30/1747486.html 1.传统修改模式,看下列代码 using (NorthwindEntit ...
- EF框架组件详述【Entity Framework Architecture】(EF基础系列篇3)
我们来看看EF的框架设计吧: The following figure shows the overall architecture of the Entity Framework. Let us n ...
- 安装Entity Framework【Setup Entity Framework Environment】(EF基础系列篇4)
Entity Framework 5.0 API是分布在两个地方:NuGet和.NET Framework中,这个.NET framework 4.0/4.5包含EF核心的API,然而通过NuGet包 ...
- Entity Framework入门教程(4)---EF中的实体关系
这一节将总结EF是怎么管理实体之间的关系.EF与数据库一样支持三种关系类型:①一对一 ,②一对多,③多对多. 下边是一个SchoolDB数据库的实体数据模型,图中包含所有的实体和各个实体间的关系.通过 ...
随机推荐
- ExtJS扩展:扩展grid之toolbar button禁用表达式
在前一篇文章我们扩展了grid通过选中记录数来禁用toolbar上的按钮,有时候我们需要通过记录中的数据来决定是否禁用按钮,今天我们就来扩展它. 照例,最新的代码和例子都在gi ...
- 基于Mono跨平台移动应用开发框架发布Xamarin 3.0
跨平台移动应用开发框架Xamarin可以让你完全用C#编写你的应用程序,在iOS.Android.Windows Phone 8.Windows8和mac平台上共享相同的代码.你可以重用你最喜欢的.N ...
- iOS开发系列--Objective-C之协议、代码块、分类
概述 ObjC的语法主要基于smalltalk进行设计的,除了提供常规的面向对象特性外,还增加了很多其他特性,这一节将重点介绍ObjC中一些常用的语法特性.当然这些内容虽然和其他高级语言命名不一样,但 ...
- 《CLR.via.C#第三版》第二部分第12章节 泛型 读书笔记(六)
终于讲到泛型了.当初看到这个书名,最想看的就是作者对泛型,委托,反射这些概念的理解.很多人对泛型的理解停留在泛型集合上,刚开始我也是,随着项目越做越多,对待泛型的认识也越来越深刻. 泛型的概念:泛型是 ...
- ASP.NET MVC 路由(一)
ASP.NET MVC路由(一) 前言 从这一章开始,我们即将进入MVC的世界,在学习MVC的过程中在网上搜索了一下,资料还是蛮多的,只不过对于我这样的初学者来看还是有点难度,自己就想看到有一篇引导性 ...
- Azure PowerShell (3) 上传证书
<Windows Azure Platform 系列文章目录> 本文介绍的是国外的Azure Global Update 2015-09-01 发现一个新的命令,在Azure PowerS ...
- entityframework使用CodeFirst创建MySql数据库出错的解决方法恢复
先告诉大家一个秘密,EF在使用 update-database 时候,使用的连接字符串来自于解决方案中的“启动项目”,而不是你在包管理器中选择的“默认项目” 0x01. 先说错误,方便大家检索到 开发 ...
- Android开发学习之路-RecyclerView使用初探
在进行一些MaterialDesign规范开发的时候,比如之前说到的CoordinateLayout实现的向上折叠效果的时候,如果依然使用ListView,那么这种效果是做不出来的,因为ListVie ...
- TypeLoadException: 找不到 Windows 运行时类型“Windows.UI.Xaml.Controls.Binding
奇怪的问题,我以为是我不小心添加了什么标签导致的,后来发现...坑爹,把项目名字改一下,然后移除掉,接着再加载一下就可以了.......崩溃了,,,,事实证明==>这个时候再把名字改回去也是不报 ...
- 【Win 10 应用开发】RTM版的UAP项目解剖
Windows 10 发布后,其实SDK也偷偷地在VS的自定义安装列表中出现了,今天开发人员中心也更新了下载.正式版的SDK在API结构上和以前预览的时候是一样的,只是版本变成10240罢了,所以大家 ...