Creating custom attributes

Writing a custom validation annotation is easier than you might expect. It’s just a static method with a specific signature. It doesn’t have to extend ValidationAttribute, and can even live in the same class definition as the entity it validates. Here are some examples:

Image

To use the custom method, you pass the class in which it’s declared and the method name to the CustomValidation attribute:

Image

[CustomValidation(typeof(MyClass), "myValidator")]

<CustomValidation(typeof(MyClass), "myValidator")>

Get Fluent Entity Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.