-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate attributes on SaveChanges not working #9662
Comments
@Jonatthu We're trying to understand the issue here. Are you saying that with your example ModelState is reporting the entity as invalid, but if you call SaveChangesAsync with your override, then it doesn't detect any validation errors? |
Exactly! SaveAsync is not detecting nothing! @ajcvickers |
Note for triage: I was able to reproduce this. The code validates |
@ajcvickers Is this going to be fix? or Is it supposed to do that? |
@Jonatthu It's not an EF issue. It may be an issue with data annotations/validation, but I'm not sure about that since I don't have enough context on how that stuff is supposed to work. I'm leaving this issue open to discuss with others. |
@ajcvickers Should I open this issue on the asp.net repository ? |
@Jonatthu We have a triage scheduled for tomorrow and I'll get back to you after that. |
@Jonatthu Looks like you need to call the TryValidateObject method with an additional bool parameter: if (!Validator.TryValidateObject(entity, new ValidationContext(entity), validationResults, true)) It works for me when I do this. |
@ajcvickers Thanks! good catch :D |
Just some additional info... When using the TryValidateObject method in EF 6.1.3, I also need to add the bool parameter for the validation to evaluate everything except [Required]. |
@ajcvickers Could you please help me understand why SaveChanges() and SaveChangesAsync() of Dbcontext class doesn't call Validator.TryValidateObject() itself? Is it because of performance?
|
@iamrahul127 Because a typical application needs to do validation before getting to the data access code. Therefore, doing validation again automatically is not appropriate. This was a mistake we made in legacy EF6. |
In addition to what @ajcvickers said, if you do want validation at the data layer, you typically want it in the database rather than in your program. For example, you can define check constraints which provide a 100% guarantee that bad data will never make it in. If you're interested in that, you may want to look at https://github.com/efcore/EFCore.CheckConstraints. |
I want to check some validation rules first when I call
SaveChanges();
My method currently looks like this trying to apply one of this solutions:
http://www.bricelam.net/2016/12/13/validation-in-efcore.html
or
#4434
And my model is:
In the controller ModelState has the error count
So this part it is working but since I am going to use view models instead the models from the database I don't want to repeat myself and use attributes in both models, so that's why I want to put the validation on save changes (following DRY).
Any solution for this?
Right now my method
SaveChangesAsync()
is not detecting any validation errors when Model State does.I am using EF Core 2
The text was updated successfully, but these errors were encountered: