Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Validation

I'm getting ready to start a new project and I've been researching the entity framework. My question is what is the best strategy for validating the entities? Other projects I've worked on have used attributes for most of the validation, but obviously this is not possible in the entity framework. Is the only way to do this by handling the partial methods in the property setters? All advice is much appreciated.

like image 836
Micah Avatar asked Oct 10 '08 12:10

Micah


People also ask

How do I validate a model in Entity Framework?

Entity Framework provides a great variety of validation features that can feed through to a user interface for client-side validation or be used for server-side validation. When using code first, you can specify validations using annotation or fluent API configurations.

What is entity validation?

Entity Framework validates all data before it is written to the database by default, using a wide range of data validation methods. However, Entity Framework comes after the user-interface data validation.

What is Framework validation?

The most obvious way of validating a quality framework is by reference to the appropriate research literature. Validation against the research literature involves identification of factors that, based on empirical research, have been identified through research to affect the effectiveness with which students learn.

What is entity validation error?

EntityValidationErrors is a collection which represents the entities which couldn't be validated successfully, and the inner collection ValidationErrors per entity is a list of errors on property level. These validation messages are usually helpful enough to find the source of the problem.


1 Answers

I have not actually used the Entity framework before but a quick search indicates that you have several options.

1) Validate at another layer in your application

Always an option, I just thought I would throw it out there explicitly.

2) Hook into the OnChanged events of the Entity then perform validation

Likely brittle and would become confusing/slow after if you have many different properties things that can change for each entity.

3) Implement partial methods to validate property changes

According to this post and this walkthrough there are partial methods available for validation. This seems like your best option as it is not very intrusive and you can selectively implement the validation you want.

I hope that helps. Good luck.

like image 93
smaclell Avatar answered Oct 07 '22 00:10

smaclell