Types of Entity in Entity Framework:

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.

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.

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 and automatic 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.

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:

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.

Entity Framework Tutorial Basics(8):Types of Entity in Entity Framework的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  2. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

  3. Entity Framework Tutorial Basics(27):Update Entity Graph

    Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...

  4. Entity Framework Tutorial Basics(22):Disconnected Entities

    Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let ...

  5. Entity Framework Tutorial Basics(15):Querying with EDM

    Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...

  6. Entity Framework Tutorial Basics(3):Entity Framework Architecture

    Entity Framework Architecture The following figure shows the overall architecture of the Entity Fram ...

  7. Entity Framework Tutorial Basics(35):Local Data

    Local Data The Local property of DBSet provides simple access to the entities that are currently bei ...

  8. Entity Framework Tutorial Basics(17):DBSet Class

    DBSet Class DBSet class represents an entity set that is used for create, read, update, and delete o ...

  9. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

随机推荐

  1. Redis 高可用及分片集群,说了你也不懂

    Redis 简介 Memcached: 优点:高性能读写.单一数据类型.支持客户端式分布式集群.一致性hash 多核结构.多线程读写性能高. 缺点:无持久化.节点故障可能出现缓存穿透.分布式需要客户端 ...

  2. codeforces D. Area of Two Circles' Intersection 计算几何

    D. Area of Two Circles' Intersection time limit per test 2 seconds memory limit per test 256 megabyt ...

  3. Zeroc Ice 负载均衡之Icegrid simple

    最近学习Icestorm的replicated例子,在本地计算机上面跑通了,但在两台机器上(一台服务器192.168.0.113,一台客户端192.168.0.188),怎么都跑不通.上网求助,大家给 ...

  4. ES6-浏览器运行环境配置方法

    现在ES6用的越来越多,想要学习使用ES6,只需简单搭建引入几个js即可运行ES6代码 但是需要基本的服务器环境下运行(如http://10.12.8.161:8047/js-test/export/ ...

  5. Agc019_C Fountain Walk

    传送门 题目大意 给定网格图上起点和终点每个格子是长为$100$米的正方形,你可以沿着线走. 平面上还有若干个关键点,以每个关键点为圆心,$10$为半径画圆,表示不能进入圆内的线,但是可以从圆周上走, ...

  6. BZOJ - 2243 染色 (树链剖分+线段树+区间合并)

    题目链接 线段树维护区间连续段个数即可.设lc为区间左端点颜色,rc为区间右端点颜色,则合并两区间的时候,如果左区间右端点和右区间左端点颜色相同,则连续段个数-1. 在树链上的区间合并可以定义一个结构 ...

  7. UVA - 1608 Non-boring sequences (分治,中途相遇法)

    如果一个序列中是否存在一段连续子序列中的每个元素在该子序列中都出现了至少两次,那么这个序列是无聊的,反正则不无聊.给你一个长度为n(n<=200000)的序列,判断这个序列是否无聊. 稀里糊涂A ...

  8. unity 联机调试(android ios)

    http://blog.csdn.net/OnafioO/article/details/44903491 (这种没用,只是在手机看到画面而已) 手机安装unityRemote并运行,unity中设置 ...

  9. 前端跨域问题,以及ajax,jsonp,json的区别

    看了很多网上的资料,小七感觉都没有完全解决我的疑惑以及问题,所以特意拿出通俗易懂的话讲解跨域问题,以及ajax,jsonp,json的区别.首先先说跨域问题什么时候需要跨域?[1]域名不同(即网址不同 ...

  10. bzoj 4503 两个串——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4503 翻转T,就变成卷积.要想想怎么判断. 因为卷积是乘积求和,又想到相等的话相减为0,所以 ...