http://www.entityframeworktutorial.net/Types-of-Entities.aspx

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.

The Entity Framework enables you to use custom data classes together with your data model without making any modifications to the data classes themselves. This means that you can use "plain-old" CLR objects(POCO), such as existing domain objects, with your data model.

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.

using System;
using System.Collections.Generic; public partial class Student
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Student()
{
this.Courses = new HashSet<Course>();
} public int StudentID { get; set; }
public string StudentName { get; set; }
public Nullable<int> StandardId { get; set; }
public byte[] RowVersion { get; set; } public virtual Standard Standard { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<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.

using System;
using System.Collections.Generic; public partial class Student
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Student()
{
this.Courses = new HashSet<Course>();
} public int StudentID { get; set; }
public string StudentName { get; set; }
public Nullable<int> StandardId { get; set; }
public byte[] RowVersion { get; set; } public virtual Standard Standard { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
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:

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.

Types of Entity in Entity Framework:的更多相关文章

  1. Entity Framework Tutorial Basics(8):Types of Entity in Entity Framework

    Types of Entity in Entity Framework: We created EDM for existing database in the previous section. A ...

  2. 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 se ...

  3. .NET Core Entity使用Entity Framework Core链接数据库

    首先安装Nuget包 Install-package Microsoft.EntityFrameworkCore Install-package Microsoft.EntityFrameworkCo ...

  4. EntityFramework 学习 一 Colored Entity in Entity Framework 5.0

    You can change the color of an entity in the designer so that it would be easy to see related groups ...

  5. Entity与Entity之间的相互转化

    一.两个实体类的属性名称对应之间的转化 1.两个实体类 public class Entity1 { private Integer id; private String name; private ...

  6. [转]Entity Framework Fluent API - Configuring and Mapping Properties and Types

    本文转自:https://msdn.microsoft.com/en-us/data/jj591617#1.2 When working with Entity Framework Code Firs ...

  7. EF(Entity Framework)系统学习系列

    好久没写博客了,继续开启霸屏模式,好了,废话不多说,这次准备重新系统学一下EF,一个偶然的机会找到了一个学习EF的网站(http://www.entityframeworktutorial.net/) ...

  8. [EF2]Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0

    http://blogs.msdn.com/b/adonet/archive/2009/05/11/sneak-preview-persistence-ignorance-and-poco-in-en ...

  9. Code First :使用Entity. Framework编程(5) ----转发 收藏

    第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...

随机推荐

  1. BZOJ2879 [Noi2012]美食节

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2879 这题codevs上也有,不过数据不同:http://codevs.cn/proble ...

  2. BZOJ1143 [CTSC2008] 祭祀river

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1143 题目大意: 给你n个点,点与点之间由有向边相连.如果u能到达v的话,那么他们就不能同 ...

  3. 剑指offer--21题

    #include "stdafx.h" #include<iostream>using namespace std; void LeftRotateString(cha ...

  4. 在当前iframe中, 获取Iframe的id

    window.frameElement   返回嵌入当前window对象的元素(比如 <iframe> 或者 <object>),如果当前window对象已经是顶层窗口,则返回 ...

  5. 哪些问题困扰着我们?DevOps 使用建议

    [编者按]随着 DevOps 被欲来越多机构采用,一些共性的问题也暴露出来.近日,Joe Yankel在「Devops Q&A: Frequently Asked Questions」一文中总 ...

  6. Selenium中expected_conditions下text_to_be_present_in_element_value方法的使用

    text_to_be_present_in_element: 判断某个元素中的text是否包含了预期的字符串 text_to_be_present_in_element_value: 判断某个元素中的 ...

  7. HTTP协议中的5类状态码

    ①   客户方错误      100   继续      101   交换协议     ②   成功      200    OK      201    已创建      202   接收      ...

  8. POJ 2027

    #include<iostream> using namespace std; int main() { int time; cin>>time; int a; int b; ...

  9. D&F学数据结构系列——前驱和后继

    前驱和后继 本文所述为二叉排序树的前驱和后继,如果想了解二叉排序树的概念,可以参考我的博文http://www.cnblogs.com/sage-blog/p/3864640.html 给定一个二叉查 ...

  10. Spine的纹理导出问题

    发现美术给过来的资源,集合到unity后,发现用Spine的默认材质Spine/Skeleton有毛边问题.对比demo的图片后发现demo的图片(都是png格式)没有白色块,而自己的图片有. 原因是 ...