Types of Entities in Entity Framework (2023)

Types of Entities in Entity Framework (1)

Previous

(Video) Entity Framework Core Part 7 - Models vs Entities

There are two types of Entities in Entity Framework: POCO Entities and Dynamic Proxy Entities.

POCO Entities (Plain Old CLR Object)

A POCO entity is a class that doesn't depend on any framework-specific base class. It is like any other normal .NET CLR class, which is why it is called "Plain Old CLR Objects".

POCO entities are supported in both EF 6 and EF Core.

(Video) Entity Framework | What is Entity Framework | Entity Framework in MVC

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.

public class Student{ public int StudentID { get; set; } public string StudentName { get; set; } public DateTime? DateOfBirth { get; set; } public byte[] Photo { get; set; } public decimal Height { get; set; } public float Weight { get; set; } public StudentAddress StudentAddress { get; set; } public Grade Grade { get; set; }}

Dynamic Proxy Entities (POCO Proxy)

Dynamic Proxy is a runtime proxy class which wraps POCO entity. Dynamic proxy entities allow lazy loading.

Note: Dynamic proxy entities are only supported in EF 6. EF Core 2.0 does not support them yet.

A 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. The ProxyCreationEnabled option must NOT be false (default is true) in context class.

The following POCO entity meets all of the above requirements to become a dynamic proxy entity at runtime.

(Video) How to Create Complex Types for entities using Entity Framework Designer

public class Student{ public int StudentID { get; set; } public string StudentName { get; set; } public DateTime? DateOfBirth { get; set; } public byte[] Photo { get; set; } public decimal Height { get; set; } public float Weight { get; set; } public virtual StudentAddress StudentAddress { get; set; } public virtual Grade Grade { get; set; }}

Note: By default, dynamic proxy is enabled for every entity. However, you can disable dynamic proxy by setting context.Configuration.ProxyCreationEnabled = false; in the context class.

At runtime, EF API will create an instance of dynamic proxy for the above Student entity. The type of dynamic proxy for Student will be System.Data.Entity.DynamicProxies.Student, as shown below:

Types of Entities in Entity Framework (2)

Use ObjectContext.GetObjectType() to find the underlying wrapped type by the dynamic proxy as shown below:

Types of Entities in Entity Framework (3)
(Video) Entity Formation: Types of Entities

Videos

1. How to Return Non Entities in Entity Framework Core with LINQ and Use Anonymous Types Instead
(Coding Under Pressure)
2. Types of Entities
(Peter Maxwell Slattery)
3. Identifying the Relationship Types between the Entity Types || Lesson 12 || DBMS || Learning Monkey
(Learning Monkey)
4. Entity Framework Best Practices - Should EFCore Be Your Data Access of Choice?
(IAmTimCorey)
5. Types of Entities
(Davis CPA Group Joplin)
6. Entity Framework vs Entity Framework Core | C#
(TechWebDots)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated: 03/21/2023

Views: 6843

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.