Rules to follow while creating a POCO class in Entity Framework
Four important rules to follow when creating a POCO class in Entity Framework. In the article, POCO vs. Entity classes, we have seen how POCO classes are different from entity classes in their persistence. We already know that these classes are persistent ignorant. To persist, we need to adapt some persistence infrastructure. EF is a common persistent infrastructure and we will use the same. Even though these classes do not inherit from the EntityObject class, EF provides change tracking information and maintains relationships for these classes. We create thios class in a persistent ignorant fashion and make use of EF functionality. You may have a question how is this possible? The answer to this question is using proxies and is discussed in a separate article.
We need to follow certain rules while creating these classes to avail the functionality of EF for persistence. For persistence, EF needs metadata. To provide metadata, we create an Entity Data Model. To understand this concept, let us select only two tables Customer and SalesOrder while creating EDM. The created model contains two entities as shown below.
Note that after we create EDM, we need to do the following. Select .edmx file in the solution explorer and select properties option. In the properties window, for the Custom Tool property, do not set any value. When the setting is done, classes will not be generated.
First, let us look at the rules we need to follow.
Rule 1: We should create these classes exactly like entities in the Model. Which means that the names and types of properties (both scalar properties and navigational) should match with those of the class. The below code represents the Customer and SalesOrder classes.
Rule 2:Navigational properties which return an EntityCollection should be of return type ICollection and marked as virtual to enable lazy loading.
Rule 3: Every property should be declared with virtual keyword in c# and with Overridable keyword in vb. When we use the property as virtual, EF creates a dynamic proxy class at runtime. This proxy class gives the functionality such as change notification, relationship fixup and Lazy loading to the POCO class. Because of the proxy classes created at runtime, these entities can be persisted back to the database successfully. This persistence process is similar to the class inherited from the EntityObject class.
Note: if we do not want change tracking with proxies, we declare only navigational properties as virtual to get lazy loading proxies. Declare all other scalar properties without a virtual keyword. We can also implement change tracking by using snapshot change tracking mechanism. I suggest not to use change tracking proxies because these proxies create problems while serializing the objects in enterprise applications.
Rule 4:Should be instantiated using ObjectContext.CreateObject method. We need to use the CreateObject method, and only then proxy object is created. The following code defines a Customer class which is created manually. This code example is created adhering to the first three rules. We will see the fourth rule when we create a proxy object. The article How to create Change tracking proxies demonstrates how to create a proxy object using CreateObject method.
We need to follow certain rules while creating these classes to avail the functionality of EF for persistence. For persistence, EF needs metadata. To provide metadata, we create an Entity Data Model. To understand this concept, let us select only two tables Customer and SalesOrder while creating EDM. The created model contains two entities as shown below.
Note that after we create EDM, we need to do the following. Select .edmx file in the solution explorer and select properties option. In the properties window, for the Custom Tool property, do not set any value. When the setting is done, classes will not be generated.
First, let us look at the rules we need to follow.
Rule 1: We should create these classes exactly like entities in the Model. Which means that the names and types of properties (both scalar properties and navigational) should match with those of the class. The below code represents the Customer and SalesOrder classes.
Rule 2:Navigational properties which return an EntityCollection should be of return type ICollection and marked as virtual to enable lazy loading.
Rule 3: Every property should be declared with virtual keyword in c# and with Overridable keyword in vb. When we use the property as virtual, EF creates a dynamic proxy class at runtime. This proxy class gives the functionality such as change notification, relationship fixup and Lazy loading to the POCO class. Because of the proxy classes created at runtime, these entities can be persisted back to the database successfully. This persistence process is similar to the class inherited from the EntityObject class.
Note: if we do not want change tracking with proxies, we declare only navigational properties as virtual to get lazy loading proxies. Declare all other scalar properties without a virtual keyword. We can also implement change tracking by using snapshot change tracking mechanism. I suggest not to use change tracking proxies because these proxies create problems while serializing the objects in enterprise applications.
Rule 4:Should be instantiated using ObjectContext.CreateObject method. We need to use the CreateObject method, and only then proxy object is created. The following code defines a Customer class which is created manually. This code example is created adhering to the first three rules. We will see the fourth rule when we create a proxy object. The article How to create Change tracking proxies demonstrates how to create a proxy object using CreateObject method.
More Resources
- POCO class in Entity Framework
- POCO vs Entity Objects with comparison table
- How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
- How to load related POCO entities - loading patterns and their differences in their usage with examples
- How to perform Lazy Loading with POCO classes
- What is Change tracking in POCOs, ways of tracking
- Snapshot change tracking with DetectChanges() method
- Change tracking with proxies, How to create proxies?
- Instantiate POCO classes using ObjectContext.CreateObject method
- Using the DetectChanges() method to Fix-up Relationship in poco entities
- Entity Framework EntityKey object
- Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
- How to create an EntityKey in Entity Framework
- Loading Related entities in EF
- How to change the state of an entity using ChangeObjectState method of ObjectStateManager
- ChangeState method of ObjectStateEntry class
- Update an entity in a disconnected scenario using ApplyCurrentValues method
- Using ApplyOriginalValues method
- ObjectStateEntry class
- Entity state in Entity Framework - what, how, why?
- AcceptAllChanges and SaveChanges methods in Entity Framework
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net