English 中文(简体)
Entity Framework Tutorial

Entity Framework Resources

Selected Reading

Entity Framework - Types
  • 时间:2024-12-22

Entity Framework - Types


Previous Page Next Page  

In Entity Framework, there are two types of entities that allow developers to use their own custom data classes together with data model without making any modifications to the data classes themselves.

    POCO entities

    Dynamic Proxy

POCO Entities

    POCO stands for "plain-old" CLR objects which can be used as existing domain objects with your data model.

    POCO data classes which are mapped to entities are defined in a data model.

    It also supports most of the same query, insert, update, and delete behaviors as entity types that are generated by the Entity Data Model tools.

    You can use the POCO template to generate persistence-ignorant entity types from a conceptual model.

Let’s take a look at the following example of Conceptual Entity Data Model.

Conceptual Entity Model

To generate POCO entities for the above Entity model −

Step 1 − Right cpck on the designer window. It will display the following dialog.

Designer Window

Step 2 − Select the Add Code Generation Item...

Code Generation

Step 3 − Select the EF 6.x DbContext Generator, write name and then cpck Add button.

You will see in your solution explorer that POCODemo.Context.tt and POCODemo.tt templates are generated.

Solution Explorer

The POCODemo.Context generates the DbContext and the object sets that you can return and use for querying, say for context, Students and Courses, etc.

Generate

The other template deals with all the types Student, Courses, etc. Following is the code for Student class which is generated automatically from the Entity Model.

namespace ConsoleApppcation1 {

   using System;
   using System.Collections.Generic;

   pubpc partial class Student {

      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
         "CA2214:DoNotCallOverridableMethodsInConstructors")]

      pubpc Student() {
         this.Enrollments = new HashSet<Enrollment>();
      }

      pubpc int ID { get; set; }
      pubpc string LastName { get; set; }
      pubpc string FirstMidName { get; set; }
      pubpc System.DateTime EnrollmentDate { get; set; }

      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
         CA2227:CollectionPropertiesShouldBeReadOnly")]

      pubpc virtual ICollection<Enrollment> Enrollments { get; set; }

   }
}

Similar classes are generated for Course and Enrollment tables from the Entity Model.

Dynamic Proxy

When creating instances of POCO entity types, the Entity Framework often creates instances of a dynamically generated derived type that acts as a proxy for the entity. IT can also be said that it is a runtime proxy classes pke a wrapper class of POCO entity.

    You can override some properties of the entity for performing actions automatically when the property is accessed.

    This mechanism is used to support lazy loading of relationships and automatic change tracking.

    This technique also apppes to those models which are created with Code First and EF Designer.

If you want the Entity Framework to support lazy loading of the related objects and to track changes in POCO classes, then the POCO classes must meet the following requirements −

    Custom data class must be declared with pubpc access.

    Custom data class must not be sealed.

    Custom data class must not be abstract.

    Custom data class must have a pubpc or protected constructor that does not have parameters.

    Use a protected constructor without parameters if you want the CreateObject method to be used to create a proxy for the POCO entity.

    Calpng the CreateObject method does not guarantee the creation of the proxy: the POCO class must follow the other requirements that are described in this topic.

    The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces because the proxy classes implement these interfaces.

    The ProxyCreationEnabled option must be set to true.

The following example is of dynamic proxy entity class.

pubpc partial class Course {

   pubpc Course() {
      this.Enrollments = new HashSet<Enrollment>();
   }

   pubpc int CourseID { get; set; }
   pubpc string Title { get; set; }
   pubpc int Credits { get; set; }
	
   pubpc virtual ICollection<Enrollment> Enrollments { get; set; }
}

To disable creating proxy objects, set the value of the ProxyCreationEnabled property to false.

Advertisements