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. python的单例模式--解决多线程的单例模式失效

    单例模式 单例模式(Singleton Pattern) 是一种常用的软件设计模式,主要目的是确保某一个类只有一个实例存在.希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场 比如,某个 ...

  2. hihocoder #1138 : Islands Travel

    题意,求1到n的最短路.不难想到单源最短路,难点在于数量级太大,因此如何建图是关键: 因为cost = min{|Xi-Xj|, |Yi-Yj|}:所以,点i的移动只有两种情况,. x距离最近的点,. ...

  3. nginx中关于并发数的问题worker_connections,worker_processes

    我认为,要搞清楚这个公式是否正确,以及如何计算的,那首先要对nginx的各个配置说明有清晰的认识: 从用户的角度,http 1.1协议下,由于浏览器默认使用两个并发连接,因此计算方法: nginx作为 ...

  4. mysql出现mysql server has gone away错误的解决办法

    应用程序(比如PHP)长时间的执行批量的MYSQL语句.执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段.比如,图片数据的处理.都容易引起MySQLserver has ...

  5. loj #6216. 雪花挂饰

    (今天碰到的题怎么这么小清新 $n$ 个不相同的点,$q$ 组询问,每次给定 $l,r$,问在 $n$ 个点中,选出 $x$ 个点 $(x \in [l,r])$,用边连起来,能构成多少种不同的树 $ ...

  6. 七、python沉淀之路--集合

    一. 1.字符串转集合 s = 'hello' se = set(s) print(se) {'e', 'o', 'h', 'l'} 2.列表转集合 l1 = ['hello','python','n ...

  7. 深入理解http/https之缓存 2

    1:web缓存的实现 web缓存: WEB缓存(cache)位于Web服务器和客户端之间. 缓存会根据请求保存输出内容的副本,例如html页面,图片,文件,当下一个请求来到的时候:如果是相同的URL, ...

  8. dockerfile mysql

    FROM centos6.6-mysql5.5:0.0.4 MAINTAINER syberos:wangmo RUN mv /etc/my.cnf /etc/my.cnf.bak ADD my.cn ...

  9. 利用PowerDesigner设计数据库

    PowerDesigner非常强大, 可以利用它完成数据库的设计. 1.下载地址:http://pan.baidu.com/s/1DsLrg 2.表设计: 建立概念数据模型(Conceptual Da ...

  10. Makefile生成

    安装:sudo yum install automake使用: 1 运行autoscan生成两个文件:autoscan.log和configure.scan.将configure.scan重命名为co ...