How to Create a POCO class in entity framework, integrate it with ObjectContext and write queries

To create a POCO class with the database-first development approach, we will first create a model for Sales Order Management system database and then create classes and make them interact with Entity Framework. There are many tables in the Sales Order Management (SOM) database. For the sake of this article, we will consider only the two tables - Customer and SalesOrder.

Step1: Create a Console Application
Creating a Console Application is a simple step and hence is not explained.
Step2: Create a Entity Data Model
Add New Item to the Console Application project and add an ADO.NET Entity Data Model (refer Fig : 1).

Add New Item properties window

In the Entity Data Model Wizard, choose Generate From Database and click Next. The Entity Data Model Wizard first asks if you want to generate a model from a database or create an empty model. Select Generate from database and click the Next button. Create a new Data Connection or choose from an existing one. The Entity Connection Settings will be stored in the App.Config file as SOMEntities.

Entity Data Model Wizard - choose your Data Connection window

In the next screen, choose Database Objects to include in the Entity Data Model (EDM) as shown below, and name the model namespace as SOMModel. Click Finish to complete the wizard.

Entity Data Model Wizard - choose your Database Objects

Now model has been created for the two tables.

Entity Data Model with entities

For simplicity purpose, I have removed some properties from both the Customer and SalesOrder entities. Now the entities appear as shown in the picture given below.

Customer and SalesOrder entities

Step3: The next step is to turn off the code generation feature
Select SOMModel.edmx file in the solution explorer and in the properties window, do not set any value for the Custom Tool property. When this setting is done, the classes will not be generated. The class file attached to the model will disappear.

Step4: Build the POCO classes manually
We have already discussed the rules to be followed to create the POCO classes. Based on these rules, we create two classes, one is Customer.cs and SalesOrder.cs. Create a Customer class and add properties as shown below. Observe that we have added properties to the Customer class for every property in the Customer entity. We declare scalar properties as simple types. For the SalesOrders Navigation property, we declare as type ICollection<T>.

Code for Customer and SalesOrder POCO classes

Observe that the class property names exactly match with entity property names. This is the first rule discussed in the article - . Using this code, EF will map the classes and entities. EF does convention based mapping which requires that Entity Type names and their property names defined in the conceptual model must match with POCO classes and their properties.

Step 5:-how to make POCO classes interact with Entity Framework
We have to integrate a POCO class with ObjectContext as it will not be able to implement persistent functionality on its own. Now that we have created EDM and classes, we will see how to make them interact with Entity Framework. As we have turned off the code generator, the ObjectContext class will not be generated. So, we have to create a class that inherits from the ObjectContext and let ObjectContext know about our classes. Now, ObjectContext class will be able to query and materialize the objects, and also save the changes back to the database.

Create a class to manage - this class can be divided into three sections - Class Declaration, Constructor and Objectset properties. The constructor of this class takes two arguments - one is EntityConnection string and name of the EntityContainer. The EntityConnection provides a connection to the EDM. We have also declared properties that return an ObjectSet of each type (Customer and SalesOrder).

code to connect a POCO class with ObjectContext

Declare Two ObjectSet properties. One property which returns an ObjectSet of Customer type. Another property, which returns an ObjectSet of SalesOrder type. ObjectSet properties are instantiated in the class constructor.

The CreateObjectSet<T> class creates a new ObjectSet<TEntity> instance. The statement _customers = CreateObjectSet<Customer>(); creates a new instance of ObjectSet of Customer and assigns to a variable _customers. This variable contains collection of Customer objects and is used to query, add, delete and modify customer objects.

Step 6: Writing Queries
In the Console application, add the below code to the main module. In the code given, we have instantiated the ObjectContext class and created a query which displays all the customers, sales orders (eager-loaded using Include method) for a particular customer.

example code ro query POCO classes