每天学一点,每天积累一天,进步就不止一点点!PS:好记性不如烂笔头,学会总结,学会思考~~~ ----要飞翔,必须靠自己!

灰太狼的梦想

好记性不如烂笔头,学会总结,学会思考~~~

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 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.

 

EF中有两种类型的实体:POCO Entity和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.

 

POCO class是一个类,它不依赖任何.NET framework的基类,它就像任何其他的普通类一样,这也是为什么被称之为“Plain Old CLR Object”的原因;

这些由实体数据模型生成的POCO实体支持大多数的增删查改的功能。下面是一个Studnet POCO实体:

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 andautomatic 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.

 

动态代理是POCO实体运行状态下的代理类,它就像POCO实体的包装类,动态包装实体支持懒加载,自动跟踪改变的特性;

一个POCO实体需要满足下面的条件,才可以成为POCO代理

1.必须定义成Public

2.不能是密封的

3.不能是抽象的;

4.导航属性必须定义成public virtual;

5.每个集合属性必须是Icollection<T>;

6.在context类中,ProxyCreationEnabled 这个选项必须不能是false(默认是true);

下面的POCO Student实体满足了上面的条件,它将会在运行的时候成为动态代理实体;

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:

注意:动态代理默认是对每个实体都是有效的,然而你可以手动禁止动态代理,通过在上下文类中设置ProxyCreationEnabled 这个选项为false,而达到目的;

context.Configuration.ProxyCreationEnabled=false;

在运行的时候Student POCO实体就会变成代理类:

代理类有两个属性,一个标量属性,一个是导航属性,

标量属性实际值包含在实体中,例如Student实体有标量属性StudentID和StudentName,这和数据库中字段一致;

导航属性指向另外相关联的实体,Student实体有一个Standard属性作为导航属性,可以保证程序从一个Student导航到相关联的standard实体。

 

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:

dynamic proxy entity in entity framework

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.

 

posted @ 2015-09-12 15:48 灰太狼的梦想 阅读(...) 评论(...) 编辑 收藏