- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Entity Framework - Types
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.
To generate POCO entities for the above Entity model −
Step 1 − Right cpck on the designer window. It will display the following dialog.
Step 2 − Select the Add Code Generation Item...
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.
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.
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