Types of Entity in Entity Framework

来源:互联网 发布:数据库刘卫国课后答案 编辑:程序博客网 时间:2024/06/05 00:16



在Entity Framwork 5.0/6.0中有两种类型实体:POCO 实体和动态代理实体

POCO(Plain Old CLR Object) 实体:


POCO类是不依赖任何框架中特殊的基类,它与其它普通的.net类类似所以才叫"Plain Old CLR Objects".
这些POCO类支持大部分的查询,插入,修改,删除那些通过Entity Data Model生成的实体操作。

  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:


Dynamic Proxy是一个POCO 实体在运行时的代理类,它可看作是POCO实体的包装类。
动态代理实体可以Lazy loading和automatic change tracking.
POCO 实体应该符合下面要求才可用作POCO proxy:
1.一个POCO 类必须被声明为public.
2.一个POCO 类必须不能是sealed.
3.一个POCO 类必须不能是abstract.
4.每个导航属性必须声明为public,virtual.
5.每个collection属性必须是ICollect<T>.
6.ProxyCreationEnabled option 必须是true(默认是true)在context类.

以下是Student POCO实体都符合上述的要求在运行时会看成Dynamic proxy entity.

 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; } }


Entity can have two types of properties, Scalar properties and Navigation properties.

Scalar properties:

Scalar properties are properties whose actual values are contained in the entity. For example, Student entity has scalar properties e.g. StudentId, 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 application to navigate from a Student to related Standard entity.


0 0
原创粉丝点击