I overridden my db.SaveChanges (), so I can call my FluentValidation validation checks before it actually tries to save it.
I have a validator for each object marked with the IValidatableEntity identifier, and if the entity matches it, it will call it and pass the objectStateEntry object.
public virtual IEnumerable<string> SaveChanges(User user)
{
List<string> validationErrors = new List<string>();
if (this.Configuration.ValidateOnSaveEnabled)
{
foreach (var entry in ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager
.GetObjectStateEntries(System.Data.Entity.EntityState.Added | System.Data.Entity.EntityState.Deleted | System.Data.Entity.EntityState.Modified | System.Data.Entity.EntityState.Unchanged)
.Where(entry => (entry.Entity is IValidatableEntity)))
{
validationErrors.AddRange(((IValidatableEntity)entry.Entity).Validate(entry));
}
}
if (!validationErrors.Any())
{ .....
The problem is that I get two different behaviors depending on how I add the object to dbContext. I assume that he only notes that the aggregate root changes and only gives him a record?
organisation.Client.Add(client);
db.Client.Add(client);
, EF , (/) ? , , EF- , .
Fluent Validations, ? , . ( db ..).