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:

  1. A POCO class must be declared with public access.
  2. A POCO class must not be sealed (NotInheritable in Visual Basic)
  3. A POCO class must not be abstract (MustInherit in Visual Basic).
  4. Each navigation property must be declared as public, virtual
  5. Each collection property must be ICollection<T>
  6. 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)的更多相关文章

  1. 7.翻译:EF基础系列---EF中的实体类型

    原文地址:http://www.entityframeworktutorial.net/Types-of-Entities.aspx 在Entity Framework中有两种实体类型:一种是POCO ...

  2. Entity Framework 教程——Entity Framework中的实体类型

    Entity Framework中的实体类型 : 在之前的章节中我们介绍过从已有的数据库中创建EDM,它包含数据库中每个表所对应的实体.在EF 5.0/6.0中,存在POCO 实体和动态代理实体两种. ...

  3. 6.翻译:EF基础系列---什么是EF中的实体?

    原文地址:http://www.entityframeworktutorial.net/basics/what-is-entity-in-entityframework.aspx EF中的实体就是继承 ...

  4. EF4.1: Add/Attach and Entity States(EF中的实体状态转换说明)

    实体的状态,连接以及 SaveChanges 方法 数据库上下文对象维护内存中的对象与数据库中数据行之间的同步.这些信息在调用 SaveChanges方法被调用的时候使用.例如,当使用 Add 方法传 ...

  5. .net Core EF统一配置实体类型

    一般情况需要对某个实体进行一些配置时代码如下: protected override void OnModelCreating(ModelBuilder modelBuilder) { base.On ...

  6. ADO.NET EF 中的实体修改方法

    http://www.cnblogs.com/zfz15011/archive/2010/05/30/1747486.html 1.传统修改模式,看下列代码 using (NorthwindEntit ...

  7. EF框架组件详述【Entity Framework Architecture】(EF基础系列篇3)

    我们来看看EF的框架设计吧: The following figure shows the overall architecture of the Entity Framework. Let us n ...

  8. 安装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包 ...

  9. Entity Framework入门教程(4)---EF中的实体关系

    这一节将总结EF是怎么管理实体之间的关系.EF与数据库一样支持三种关系类型:①一对一 ,②一对多,③多对多. 下边是一个SchoolDB数据库的实体数据模型,图中包含所有的实体和各个实体间的关系.通过 ...

随机推荐

  1. PHP好任性 —— 大小写敏感有两种规则,然而并没有什么特别原因

    大小写敏感 变量.常量大小写敏感 大小写不敏感 类名.方法名.函数名.魔法变量大小写不敏感 原因 有人原引了Rasmus 在一次会议上的发言大意: "I'm definitely not a ...

  2. CYQ.Data V5 MDataTable 专属篇介绍

    前言 以前一两个月才出一篇,这三天有点变态地连续1天1篇(其实都是上周末两天写好的存货). 短期应该没有新的和此框架相关的文章要写了,这应该是最后一篇,大伙且看且珍惜. 前两篇讲数据库读写分离和分布式 ...

  3. 循序渐进做项目系列(4)迷你QQ篇(2)——视频聊天!(附源码)

    一·效果展示 源码派送:MiniQQ1.1 文字聊天的实现参见:循序渐进做项目系列(3):迷你QQ篇(1)——实现客户端互相聊天 二·服务端设计 对于实现视频聊天而言,服务端最核心的工作就是要构造多媒 ...

  4. 《深入理解Java虚拟机》垃圾收集器

    说起垃圾收集(Garbage Collection,GC),大部分人都把这项技术当做Java语言的伴生产物.事实上,GC的历史远比Java久远,1960年诞生于MIT的Lisp是第一门真正使用内存动态 ...

  5. Mysql 主从延时监控

    200 ? "200px" : this.width)!important;} --> 介绍 主从延时在主从环境中是一个非常值得关注的问题,有时候我们可以通过show sla ...

  6. MapReduce剖析笔记之一:从WordCount理解MapReduce的几个阶段

    WordCount是一个入门的MapReduce程序(从src\examples\org\apache\hadoop\examples粘贴过来的): package org.apache.hadoop ...

  7. ASP.NET MVC 路由(五)

    ASP.NET MVC 路由(五) 前言 前面的篇幅讲解了MVC中的路由系统,只是大概的一个实现流程,让大家更清晰路由系统在MVC中所做的以及所在的位置,通过模糊的概念描述.思维导图没法让您看到路由的 ...

  8. JS数字计算精度误差的解决方法

    本篇文章主要是对javascript避免数字计算精度误差的方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助. 如果我问你 0.1 + 0.2 等于几?你可能会送我一个白眼,0.1 + 0. ...

  9. C#设计模式系列:命令模式(Command)

    1.命令模式简介 1.1>.定义 命令模式的目的是解除命令发出者和接收者之间的紧密耦合关系,使二者相对独立,有利于程序的并行开发和代码的维护.命令模式的核心思想是将请求封装为一个对象,将其作为命 ...

  10. 解密jQuery事件核心 - 绑定设计(一)

    说起jQuery的事件,不得不提一下Dean Edwards大神 addEvent库,很多流行的类库的基本思想从他那儿借来的 jQuery的事件处理机制吸取了JavaScript专家Dean Edwa ...