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. APP登录的机制

    1.APP每次发送请求时,都会发送header给服务器,服务器去校验传过来的信息是否正确:校验成功后登录成功,若传入的信息不符合该用户的信息则服务器判断,传给APP登录失败 每次的请求都会传入上图中的 ...

  2. UVA - 11754 Code Feat (分块+中国剩余定理)

    对于一个正整数N,给出C组限制条件,每组限制条件为N%X[i]∈{Y1,Y2,Y3,...,Yk[i]},求满足条件的前S小的N. 这道题很容易想到用中国剩余定理,然后用求第k小集合的方法输出答案.但 ...

  3. poj2411 Mondriaan's Dream[简单状压dp]

    $11*11$格子板上铺$1*2$地砖方案.以前做过?权当复习算了,毕竟以前学都是浅尝辄止的..常规题,注意两个条件:上一行铺竖着的则这一行同一位一定要铺上竖的,这一行单独铺横的要求枚举集合中出现连续 ...

  4. 下载安装jdk

    阿里云服务器部署第一步:下载-安装jdk 1.下载路径:http://www.oracle.com/ 2.通过windows远程桌面连接上服务器:连接方式: 然后在服务器上c:/user/下创建jav ...

  5. Inception-Resnet-V2

    零.Inception-Resnet-V2的网络模型 整体结构如下,整体设计简洁直观: 其中的stem部分网络结构如下,inception设计,并且conv也使用了7*1+1*7这种优化形式: inc ...

  6. !heap 和 _HEAP_ENTRY

    WinDBG提供了!heap命令帮助我们查找heap,同时我们也可以通过dt和MS SYMBOL来了解memory layout. 假设我们有下面一个小程序. int _tmain(int argc, ...

  7. HIVE-默认分隔符的(linux系统的特殊字符)查看,输入和修改

    这段时间做hive的时候,用到了系统默认分隔符.通常下面2中情况我们需要需要用到分隔符 1,制作table的输入文件,有时候我们需要输入一些特殊的分隔符 2,把hive表格导出到本地时,系统默认的分隔 ...

  8. Angular5学习笔记 - 虚拟RestfulApi配置与使用(六)

    一.安装json-server功能 #windows cnpm install json-server -g #Mac & Linux sudo npm install json-server ...

  9. hihoCoder#1139(二分+bfs)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回和上上回里我们知道Nettle在玩<艦これ>,Nettle在整理好舰队之后终于准备出海捞船和敌军交战了 ...

  10. mysql函数之三:find_in_set() 函数 返回str在字符串集strlist中的序号

    如果字符串str是在的strlist组成的N子串的字符串列表,返回值的范围为1到N. FIND_IN_SET(str,strlist) 如果字符串str是在的strlist组成的N子串的字符串列表,返 ...