I always see a DbContext.SaveChanges()
in the UnitOfWork.Commit()
method. But when I fetch an entity from the DbContext and change property, the UnitOfWork.Commit()
will write the new entity values back to my database without any validation.
I will avoid to use the integrated DbContext.Configuration.ValidateOnSaveEnabled = true;
.
Currently I don't use the UnitOfWork-Pattern. I run the validation on every Insert/Update-operation in my Service classes and if the validation was successful the Repository calls DbContext.SaveChanges()
.
How to avoid a UnitOfWork.Commit()
with invalid entities?
If I use the UnitOfWork object in the ASP.NET MVC Controller, as it suggest in this answer:
Where does Unit Of Work belong w/ EF4, IoC (Unity), and Repository?
...there is no guarantee that the validation was done.