Mark Mark - 2 months ago 34
C# Question

Best practice to implement business logic validation - Entity Framework

I'm using Entity Framework for the first time, and I need to add business logic before inserting new objects into the db, here are the options I thought about:

  1. Implement business logic on the DataContext level - by overriding SaveChanges method

  2. Implement business logic for each entity using OnPropertyChanging partial method

  3. Wrap the generated code in a custom class that implement the validation layer.

Which method is best practice when managing business logic on Entity Framework


Have a look at validation with EF - the validation is inside the entities themselves.

It's a very clean way to organise your project.

When you have POCOs, the obvious place for entity validation is in the POCO itself.

It makes sense that any validation of the Customer object is actually in the Customer class.