Types of Entity in Entity Framework:
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:
- A POCO class must be declared with public access.
- A POCO class must not be sealed (NotInheritable in Visual Basic)
- A POCO class must not be abstract (MustInherit in Visual Basic).
- Each navigation property must be declared as public, virtual
- Each collection property must be ICollection<T>
- 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:的更多相关文章
- 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 ...
- 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 ...
- .NET Core Entity使用Entity Framework Core链接数据库
首先安装Nuget包 Install-package Microsoft.EntityFrameworkCore Install-package Microsoft.EntityFrameworkCo ...
- 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 ...
- Entity与Entity之间的相互转化
一.两个实体类的属性名称对应之间的转化 1.两个实体类 public class Entity1 { private Integer id; private String name; private ...
- [转]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 ...
- EF(Entity Framework)系统学习系列
好久没写博客了,继续开启霸屏模式,好了,废话不多说,这次准备重新系统学一下EF,一个偶然的机会找到了一个学习EF的网站(http://www.entityframeworktutorial.net/) ...
- [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 ...
- Code First :使用Entity. Framework编程(5) ----转发 收藏
第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...
随机推荐
- hdu 2426 Interesting Housing Problem 最大权匹配KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2426 For any school, it is hard to find a feasible ac ...
- Leetcode#137 Single Number II
原题地址 遍历所有数字,统计每一位出现的次数,模3即为只出现一次的数字在那一位的情况. 代码: int singleNumber(int A[], int n) { ] = {}; ; i < ...
- libxml2 crash
在 xmlParseFile 就那么挂在里面了 只有一个dll 和一堆头文件 没法调试 有如下猜想(感谢fb 和 array) 这是一个unhandled exception可以catch看看 也许是 ...
- The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near
The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near ...
- iis express 启动多个网站
iis express一次只能运行一个网站, 执行iisexpress 不加参数. 将执行配置文件中的第一个网站. iis express一次只能运行一个 应用程序池. 可以使用这个特点实现一次 ...
- Codeforces Round #204 (Div. 2)->C. Jeff and Rounding
C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...
- SQL Server 2008 R2评估期已过的解决办法
SQL Server 2008 R2评估期已过的解决办法 发现问题 北美产品测试服每日随机任务没有刷新 每日随机任务是使用数据库作业定期执行操作,重置玩家随机任务项 排查问题 www.2cto. ...
- yum rpm 命令 linux
centos的软件安装大致可以分为两种类型: [centos]rpm文件安装,使用rpm指令 类似[ubuntu]deb文件安装,使用dpkg指令 [centos]yum安装 类似[ubuntu ...
- MAC OS下使用Xcode进行GLSL编程的配置过程
整理自之前使用的163博客原创文章. GLSL项目中需要使用GLEW库,因此先要安装GLEW库和在Xcode中配置GLEW.要使GLEW在Xcode中被正确链接,又需要通过MacPorts来安装GLE ...
- iOS-CALayer实现简单进度条
/** * 用CALayer定制下载进度条控件 * 1.单独创建出CALayer * 2.直接修改CALayer的frame值,执行隐式动画,实现进度条效果 * 3.用定时器(NSTimer) ...