This article demonstrates several techniques for using partial C# classes to address common issues in EF 8/ASP.NET8.
\ Abstract: In EF 8 – Database First approach the generated EF classes cannot be directly extended with additional functionality since they are overwritten when the model is regenerated. To overcome this, we can leverage partial C# classes. This article presents useful tricks for extending functionality in an EF/ASP.NET environment.
1 Entity Framework Core – Database First ApproachIn my C#/ASP.NET 8 MVC project, I am using the Entity Framework Core Database First approach. This is necessary because several applications across different technologies rely on the same SQL Server database. To generate a model from the database, I use EFCorePowerTools.
\ The issue arises when the generated EF model needs to be updated to reflect changes to the database schema. Since the generated EF entity classes will be overwritten, any modifications made to them are lost. This is a problem because, at times, we need to extend the EF entities for use within the application.
\ To solve this issue, I rely on tricks involving partial C# classes, which will be explained below.
2 A Typical Situation in C#/EF/ASP.NET appIn a typical C#/EF/ASP.NET 8 MVC application, the situation might look like the following code:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //this is typical class generated by EF Core Power Tools //in EF-Database-First approach //Here, Customer-partial-class-1 is a class generated by EF (via reverse engineering from the database). It includes some validation attributes that correspond to database constraints. This class is usually used in a model class (e.g., CustomerEdit_ViewModel), where the validation attributes are processed during an action or method (e.g., CustomerEdit).
3 Trick 1 – Use Partial Classes to Add Custom PropertiesIf we need to add properties to the generated class, we can't directly modify Customer-partial-class-1, because doing so would lead to the changes being overwritten. Instead, we can create Customer-partial-class-2 and add our custom properties there. To prevent EF from including these properties in the model, we must use the [NotMapped] attribute.
\ Here is an example:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //this is typical class generated by EF Core Power Tools //in EF-Database-First approach //The code, with detailed comments, should be self-explanatory.
4 Trick 2 - Use Partial Classes to Add Custom Validation AttributesSometimes, the automatically generated validation attributes in EF are not sufficient, and we need to add custom validation rules. Again, partial classes come to the rescue. Below is an example:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //this is typical class generated by EF Core Power Tools //in EF-Database-First approach //As you can see, we modified Customer-partial-class-2 and added a Customer_MetaData class. The purpose here is to add additional validation for the NAME property, requiring a minimum of 3 characters.
\ In ASP.NET MVC, we can use ModelState to validate and retrieve error messages. Note that we also had to use the utility method ValidationUtil.ValidateModelForCustomRules to manually trigger custom validation.
\ The code and comments should provide all the necessary details for understanding this technique.
5 ConclusionIn my C#/EF/ASP.NET 8 MVC application, I use the above techniques with partial C# classes to extend the functionality of EF-generated classes. These tricks simplify common scenarios, making the code more concise and easier to maintain.
All Rights Reserved. Copyright 2025, Central Coast Communications, Inc.