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. 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:
- 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.
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的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- Entity Framework Tutorial Basics(27):Update Entity Graph
Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...
- Entity Framework Tutorial Basics(22):Disconnected Entities
Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let ...
- Entity Framework Tutorial Basics(15):Querying with EDM
Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...
- Entity Framework Tutorial Basics(3):Entity Framework Architecture
Entity Framework Architecture The following figure shows the overall architecture of the Entity Fram ...
- Entity Framework Tutorial Basics(35):Local Data
Local Data The Local property of DBSet provides simple access to the entities that are currently bei ...
- 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 ...
- 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 ...
随机推荐
- Django 框架概况
Django 里更关注的是模型(Model).模板(Template)和视图(Views),称为 MTV模式: M 代表模型(Model),即数据存取层. 该层处理与数据相关的所有事务:如何存取.如何 ...
- HihoCoder1080 更为复杂的买卖房屋姿势(线段树+多重lazy)
描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们可以化身上帝模式,买卖房产. 在这个游戏里,会不断的发生如下两种事件:一种是房屋自发的涨价或者降价,而另一种是政 ...
- [BZOJ1797][AHOI2009]最小割Mincut
bzoj luogu sol 一条边出现在最小割集中的必要条件和充分条件. 先跑出任意一个最小割,然后在残余网络上跑出\(scc\). 一条边\((u,v)\)在最小割集中的必要条件:\(bel[u] ...
- 2017.10.3北京清北综合强化班DAY3
括号序列(bracket) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一个括号序列,但这个序列不一定合法. 一个合法的括号序列如下: ()是合法的 ...
- (转)SC命令---安装、开启、配置、关闭windows服务 bat批处理
本文转载自:http://blog.csdn.net/moruna/article/details/9190733 废话不多说,看命令行更直接! 一.直接使用cmd来进行服务的一些操作 1.安装服务 ...
- 模块化方案esl以及amd的依赖方式
来自AMD设计思想的总结和思考 在之前了解es6模块化的时候有遇到过依赖循环的问题,在es6中对于模块是引用性的,而当时于es6模块化做对比的commonjs(CMD规范)对于模块是值类型(会将其缓存 ...
- 转:InnoDB Crash Recovery 流程源码实现分析
此文章转载给登博的文章,给大家分享 InnoDB Crash Recovery 流程源码实现分析 Crash Recovery问题 本文主要分析了InnoDB整个crash recovery的源码处理 ...
- maven学习6 Eclipse下Tomcat常用设置
Eclipse下Tomcat常用设置 1,Eclipse建立Tomcat服务 1.1 新建Server 首先这里是指,jee版的Eclipse.Eclipse是没有像MyEclipse那样集成Tomc ...
- 问题:oracle 计算年龄;结果:oracle中根据生日计算年龄的问题
SELECT FLOOR(MONTHS_BETWEEN(SYSDATE,birthday)/12,1) FROM ltteacherinfo where name='朱雪东111'这个报错ORA 00 ...
- C语言学习笔记--动态内存分配
1. 动态内存分配的意义 (1)C 语言中的一切操作都是基于内存的. (2)变量和数组都是内存的别名. ①内存分配由编译器在编译期间决定 ②定义数组的时候必须指定数组长度 ③数组长度是在编译期就必须确 ...