entity framework – Is it considered a bad practice to include methods in classes that comprise an EF model?

There is no technical limitation here. EF entity classes can have methods. Those methods do not impact the de/serialization of the entities, so there’s no conflict.

That being said, it raises red flags and is generally not a good idea.

For one, your persistence logic and business/domain logic should be separated layers. Since the EF entity belongs to the persistance layer, and validation (or general operations) belong to the business/domain layer, these logically shouldn’t find themselves in the same class definition.

Is it better to create a repository for the class, and implement validation in the repository?

EF brings with it some basic data validation, but I would not use that as my business validation, but rather as a last line of defense in case the business logic fails us in some unexpected way.

I would instead validate the DTO which is passed to the business layer, and use that as my main validation logic. When validation passes, the business layer can then talk to the persistence layer and the persistence layer (usually a repository) doesn’t need to explicitly validate anything further.

That being said, if you have an application with several data stores, and those stores have different validation rules, there can be value from having the persistence layer do its own round of validation. But that’s not the most common scenario. The main business validation should generally be situated in the business layer, not the persistence layer.

Or to create validation classes per entity?

Generally speaking, I do separate my domain models from their validation logic. As far as I’m concerned, these are two separate responsibilities and it’s beneficial to keep them separated.

This ensures that validation rules can be altered without impacting the model itself. This can be useful either during development, or if your application has a dynamic validation set that can be altered during runtime (for example, we make applications where the admin end users can set their own validation rules, so we dynamically “pick and mix” our validators).

As per Ben Cottrell’s suggestion in the comments, I would also suggest looking at FluentValidation as it helps you in keeping your validation logic separate from the rest of your logic.


I think there is no “best” approach, but in your opinion, where should the Validation method be implemented?

Context is key here. If this is a quick and dirty home project with no long life expectancy, just bang it in wherever it works. I do not apply my own clean coding advice to small tools I hack together once in a while, as the effort does not outweigh the benefit if I don’t intend to use the application for a long time.

If this is a professional project or a personal project where long term longevity is a relevant consideration, at the very least keep the persistence and business/domain logic separated.

If this is an enterprise-grade project, I’d advocate keeping the (business/domain) validators separate from the business logic/DTOs themselves to further promote maintainability and loose coupling.