zoukankan      html  css  js  c++  java
  • 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:

    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.

    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.

  • 相关阅读:
    Firefox做默认浏览器,点击QQ面板连接(QQ邮箱,空间),延迟很久很久才打开网页(Firefox 浏览器 延迟 打开 点击没反应) 拂晓风起
    web.xml filter执行顺序 java jsp web 拂晓风起
    JAVA 取得当前目录的路径/Servlet/class/文件路径/web路径/url地址 拂晓风起
    Javascript 检测 页面是否在iframe中 拂晓风起
    Spring 获取web根目录 (Spring线程获取web目录/路径/根目录,普通类获取web目录) 拂晓风起
    java输出字符串到多个输出流 同时输出到console终端,网页,文本 拂晓风起
    开启eclipse全部代码提示,自动完成(类似visual studio 2008) 拂晓风起
    java web 自定义错误页面 完整jsp错误页面代码(同时写错误日志) error.jsp 拂晓风起
    struts/Servlet,action转到jsp后,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径) 拂晓风起
    C# 14位日期型字符串yyyyMMddHHmmss转变为日期格式
  • 原文地址:https://www.cnblogs.com/chucklu/p/5114066.html
Copyright © 2011-2022 走看看