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不仅影响模型也影响数据库.在这一章,你将 ...
随机推荐
- DEVICE DRAW VERTEX BUFFER TOO SMALL
D3D11 WARNING #356 这个傻warning的意思看起来是说vertex buffer 太小了 描述是这样的: Vertex Buffer at the input vertex slo ...
- PE文件之资源讲解
资源是PE文件中非常重要的部分,几乎所有的PE文件中都包含资源,与导入表与导出表相比,资源的组织方式要复杂得多,要了解资源的话,重点在于了解资源整体上的组织结构. 我们知道,PE文件资源中的内容包括: ...
- 三维云模拟Three.js
http://www.mrdoob.com/#/131/clouds http://www.webgl.com/2012/03/webgl-demo-clouds/ <!DOCTYPE html ...
- CkEditor 插件开发
CKEditor的插件开发其实很简单只需要两步.1.通过CKEditor.plugins.add()方法编写插件的逻辑主体, 2.告诉CKEditor我们有一个自定义插件需要添加进来. //创建插件逻 ...
- C#索引器及示例
public class IndexSeletor<T> where T:struct { private List<T> _listObj; public IndexSele ...
- 传说中的WCF(10):消息拦截与篡改
我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过 程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过 ...
- iframe父子兄弟之间调用传值(contentWindow && parent)
iframe的调用包括以下几个方面:(调用包含html dom,js全局变量,js方法) 主页面调用iframe: iframe页面调用主页面: 主页面的包含的iframe之间相互调用: 主要知识点 ...
- lintcode :单词搜索
题目 单词搜索 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. 样 ...
- lintcode :最长公共子串
题目 最长公共子串 给出两个字符串,找到最长公共子串,并返回其长度. 样例 给出A=“ABCD”,B=“CBCE”,返回 2 注意 子串的字符应该连续的出现在原字符串中,这与子序列有所不同. 解题 注 ...
- java 语法错误 (操作符丢失) 在查询表达式
遇到的详细问题: a[0]="11"; a[1]="2223"; a[2]="333"; sta.executeUpdate("i ...