A Beginner’s Tutorial on Performing Validations in Entity Framework using Partial Methods

This article talks about performing validations while using Entity framework. We will see how the Entities facilitate validations using partial methods. We will also briefly look into the concept of partial classes and partial methods in C#.

Background

There are many a times when we need to perform the business rule validations while inserting or updating the data in a database. While using classic ADO.NET this can be done easily in Business Logic Layer of Data Access Layer.

If we are using Entity framework in our Data Access Layer then how can we ensure that the business rule validations are followed while working with entities. Entity framework provides us a beautiful mechanism for performing validations. This is facilitated by the use of partial functions. Let is look at how we can use the partial functions and perform validations with entity framework.

Using the code

A look into the partial classes and partial methods.

C# provides the flexibility of splitting the classdefinitions across multiple files using partial methods. This was particularly useful when the IDE generated code was used in unison with the custom code. we can simply split the class definition in multiple places by using the partialkeyword. Following code shows how the partial classes can be defined in C#.

The concept of partial methods take this approach one step further. It gives me a possibility of creating the declaration of a methods in one partial class and the definition can be provided in another partial class. So let us add a partial method in the first partial Person class in the above code snippet.

What we have done now is that we have created a partial method OnDesignationChangingin the partial class and I am calling this methods whenever someone is setting the designation from outside. Right now the function call will simply be ignored as no definition for the function is present.

So let us not provide the definition of this function in the other partial class so that whenever the function is invoked the definition we provided will execute.

Now lets see how this function defined in second partial class runs in unison with the existing partial class having the partial method declaration.

partialMethod

So now we can safely say that the partial methods can be used as hooks on the existing classes. the original class can define a partial method and we can hook in our functionality by implementing this partial method.

Recap on Entity framework CRUD Operations

Let us start by creating a small application to perform the basic CRUDoperations using entity framework. We will define a small Database with single table for storing the data of Bikeand Manufacturer’s name.

dbTable

We will then have a class library with entity ADO.NET entities added in this library which will let us access this database. The final application will be a console application that will perform the basic CRUD operations on this table.

Note: Please see the attached sample project for details. Here is only the specific code snippet for CRUD operation only.

Lets see this code in action:

EFRunninScreenShot

Important: The sample project contains the complete Data access layer with entity framework and the console application with full code. To run the sample application just create the database and the table as shown above, change the configuration string in App.config of the console application and the application will run.

Note: To read more about the CRUD operations using Entity Framework refer: An Introduction to Entity-Framework for Absolute Beginners

Validations using Entity framework

Now if we look at the generated entities, we can see that the entity classes provides partial methods like OnChangingand OnChanged.

PartialMethodShowing

What we need to do is to simply create a partial class with the same name as the Entity class and inside that class implement this partial method. In this partial method we can write our custom validation logic to validate against the business rules.

Let us say we want to perform a validation that the user should not be able to add a manufacturer name with numbers. To do that we need to implement the the OnManufacturerChanging function. So let us add a partial class with the same name as our Entity i.e. Bikeand then implement this function.

Now whenever we will try to add or update the data with manufacturer name containing a number the exception will be thrown which we can handle in our application and then notify the user.

efValidating

This way we can put in hooks whenever the entities are changing or changed. This provides a very clean way of handling the validation scenarios while using entity framework.

Note: To run the solution, please create the Database with the above shown table and then change the database path of ConnectionString in the App.config file.

Point of interest

We tried to look into the partial methods and partial functions and how the partial functions can be used as hooks with the classes. We saw how we can use the partial functions provided by Entity framework to perform custom validations.

Download sample code for this article:  EntityFrameworkSamplePartialClassAndMethodsDemo