Hi, What is the correct way to do Business Validation on entities with Entity Framework 6. By business Validation I mean validation that require more than one entity in process with, sometime, a little more complex rules that involve all these entities. A validation that require the system to call he database. I already checked this post: [ScottGu's Blog - Class-Level Model Validation with EF Code First and ASP.NET MVC 3](https://weblogs.asp.net/scottgu/class-level-model-validation-with-ef-code-first-and-asp-net-mvc-3) But, like most example about EF and validation all validations are limited to simple check that consider only the current entity. I need to do some check based on other entities. Example: I have a order
entity that contain a quantity of product but this quantity must be a multiple of a variable stored in my item
entity. This is what I call the business validation. To perform this validation on order I need to go in database and check the item selected for this order.
public class Order : IValidateObject
{
public int Id { get; set; }
public int ItemId { get; set; }
public int Quantity { get; set; }
public IEnumerable Validate(ValidationContext validationContext)
{
var item = DbContext.Items.Find(ItemId); // <-- What about this?
if (Quantity % item.Multiple != 0)
yield return new ValidationResult("Quantity not multiple of item", new\[\] {Quantity});
}
}
public class Item
{
public int Id { get; set; }
public int Multiple { get; set; }
}
How should I implement this kind of business validation that require other entities? I'm more looking for a good tutorial about this subject. I search but each time I think I find the subject the tutorial is limited to classic validation with Data Annotation or other type of classic validation. Maybe you will juste tell me there is no problem to class my context in the Validate method. It is just that most of tutorial separate the entity layer from the context layer suggesting entities must only be simple POCO with no behavior. I most and most disagree with this but I feel alone.