In this article, I am going to discuss the Entity Types in Entity Framework in detail. Please read our previous article where we discussed Entities in Entity Framework with an example. At the end of this article, you will understand the Different Types of Entities in Entity Framework and their use with examples.
Types of Entities in Entity Framework:
In Entity Framework, there are two types of entities that allow developers to use their own custom data classes together with the data model without making any modifications to the data classes themselves. The two types of Entities are as follows:
POCO stands for Plain Old CLR Objects which can be used as existing domain objects with your data model. The POCO data classes which are mapped to entities are defined in a data model. 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”. The POCO entities are supported in both EF 6 and EF Core.
These POCO entities 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 an Employee POCO entity.
The Dynamic Proxy is a runtime proxy class that wraps the POCO entity. Dynamic proxy entities allow lazy loading.
The following POCO entity meets all of the above requirements to become a dynamic proxy entity at runtime.
We can find the original or actual entity type from this proxy type using the GetObjectType method of ObjectContext. This is a static method so there is no need to create an object of the object context.
Note: By default, the dynamic proxy is enabled for every entity. However, you can disable dynamic proxy in the context by setting this.Configuration.ProxyCreationEnabled = false; as shown below
Summary:
I hope this post will be helpful to understand the concept of Entity Types in Entity Framework
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
Types of Entities in Entity Framework:
In Entity Framework, there are two types of entities that allow developers to use their own custom data classes together with the data model without making any modifications to the data classes themselves. The two types of Entities are as follows:
- POCO entities
- Dynamic Proxy entities
POCO stands for Plain Old CLR Objects which can be used as existing domain objects with your data model. The POCO data classes which are mapped to entities are defined in a data model. 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”. The POCO entities are supported in both EF 6 and EF Core.
These POCO entities 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 an Employee POCO entity.
public class Employee { public int ID { get; set; } public string Name { get; set; } public string Email { get; set; } public string Gender { get; set; } public Nullable<int> Salary { get; set; } public Nullable<int> DepartmentId { get; set; } public virtual Department Department { get; set; } }Dynamic Proxy Entities
The Dynamic Proxy is a runtime proxy class that wraps the POCO entity. Dynamic proxy entities allow lazy loading.
- A POCO entity should meet the following requirements to become a POCO proxy:
- The POCO class must be declared with public access.
- A POCO class must not be sealed.
- The POCO class must not be abstract.
- Each navigation property must be declared as public, virtual.
- All the collection property must be ICollection
. - 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.
public class Employee { public int ID { get; set; } public string Name { get; set; } public string Email { get; set; } public string Gender { get; set; } public Nullable<int> Salary { get; set; } public Nullable<int> DepartmentId { get; set; } public virtual Department Department { get; set; } }Get the Entity Type from the Proxy Type:
We can find the original or actual entity type from this proxy type using the GetObjectType method of ObjectContext. This is a static method so there is no need to create an object of the object context.
namespace EFDemo { class Program { static void Main(string[] args) { using (EF_Demo_DBEntities DBEntities = new EF_Demo_DBEntities()) { var employee = DBEntities.Employees.Find(1); var employeeType = ObjectContext.GetObjectType(employee.GetType()); Console.WriteLine("FullName of Employee Master Type :" + employeeType.FullName); Console.ReadKey(); } } } }The GetObjectType returns an actual entity type even if we pass the instance of entity type (not the proxy type). In short, we can always use this method to get the actual entity type, there is no need to check whether the type is a proxy type or not.
Note: By default, the dynamic proxy is enabled for every entity. However, you can disable dynamic proxy in the context by setting this.Configuration.ProxyCreationEnabled = false; as shown below
namespace EFDemo { using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class EF_Demo_DBEntities : DbContext { public EF_Demo_DBEntities() : base("name=EF_Demo_DBEntities") { this.Configuration.ProxyCreationEnabled = false; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Department> Departments { get; set; } public virtual DbSet<Employee> Employees { get; set; } } }In the next article, I am going to discuss the Entity States in Entity Framework. Here, in this article, I try to explain Entity Types in Entity Framework. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article
Summary:
I hope this post will be helpful to understand the concept of Entity Types in Entity Framework
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.