Entity Framework using Partial classes to add business logic and validation to generated entities

With Entity Framework you can use partial classes and partial methods to add business logic or override certain methods of the generated Entity Framework entities. You can find the default MSDN information about partial classes here :
http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx

In my project I have a ADO.NET Entity Data Model which looks as following:

Entity Framework ADO.NET Entity Data Model

If we look at the generated entity code for the entity Post in the designer code:

Entity Framework partial entities

You can notice the class Post is marked with the partial keyword. This allows you to attach code to this generated class. If you would add code to this generated entity directly into the generated code, the next time you update the ADO.NET Entity Data Model, you would lose the changes you made, since it is generated code. To add Methods to the Post class that can persist, even if you update the model, you can make use of partial classes.

Entity Framework partial classes adding code to entity

In the same library as which your ADO.NET Entity Data Model belongs to, you can create a partial class Post. It is important that the partial class Post belongs to the same namepace as the partial class Post that is generated in the designer of the model. At runtime, the class is being constructed with the results of both these 2 partial classes into 1 single class.

In our partial class we created, we added for example a business logic operation WritePost() which in our case simply writes the output of the variables of the class to the output window. Notice that even though the _Title and _Id properties are not defined in my partial class I created, they are accessible to this partial class since those properties are defined to the other partial class that was generated. That means in the newly created partial class you can add logic that works on properties and variables defined in the other partial class.

If we write some code to test this behavior:

Entity Framework partial classes

By default the WritePost method is not defined in our generated entity, but by adding a partial class to our project with the same name and namespace, we are able to add custom logic to our generated entity. Next time you update the ADO.NET Entity Data Model, your changes will persist.

Executing the console application behaves as expected:

Entity Framework partial classes

If you look into the generated entity code, you will also find partial methods back, on which you can attach logic:

Entity Framework partial classes

We have a OnTitleChanging(string value) partial operation. If for example we would to add some validation logic every time someone changes the Title of a Post entity, we could do with writing a partial operation in our partial class:

Entity Framework partial onChanging onChanged operations

If we now try to change the value of the Title property of a post entity in to a value that is less then 10 characters, a notification gets written to the output window:

Entity Framework partial classes

Result:

Entity Framework partial classes

Any suggestions, remarks or improvements are always welcome.
If you found this information useful, make sure to support me by leaving a comment.

Cheers and have fun,

Robbin

About these ads