Chapter 3. Handling Validation in Entity Framework

In this chapter, we will cover the following recipes:

  • Validating simple properties
  • Validating complex properties
  • Validating collection properties
  • Creating custom property validation
  • Improving MVC UI with entity framework validation

Introduction

When we sell a car, do we just take it on faith that the stack of bills we were given show the correct amount, or do we count them to validate it? Often, this validation has little to no effect on the storage system. There are two levels of validation in this scenario. When we count the money, we are doing content validation to ensure that it meets the requirements. When we put it in our wallet, we are doing structure validation . Is it the right size and shape to meet the storage requirements? This is no different than the approach to data validation that we should take when working with the Entity Framework. We can enforce certain restrictions on the content of the data objects, and we can specify different restrictions on the structure of the data in configuration.

The content restrictions are more subjective, and often conditional. We want these rules to be enforced at the same time as the structure restrictions, so our validation is executed together and before any database calls have been made. All of these restrictions will specify some kind of business rule, but will not be tied directly to the structure of the code. These are the topics that this chapter will cover.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset