开发者

How to prevent EF from validating properties that are not mapped during DBContext.SaveChanges()

I have a user model with two [NotMapped] string properties Password and ConfirmPassword. These are unmapped because I save password as byte array (after salting) so there are two additional properties (mapped) InternalPassword and Salt in user model.

The problem is when I use the user model to change password, entity framework throws DBEntityValidation error stating "The Password property is required." What I understand here is that EF is trying to validate my model before saving and since Password/ConfirmPassword are not set, it is throwing 开发者_运维问答this error. This raises following questions:

1) If property Password is explicitly annitated as [NotMapped], why is EF validating it during save? 2) IF EF performs validation during save, and the same is also performed during binding (I.E. in the controller action method), does it not hurt performance? (validating twice) 3) What's the recommended way to resolve this error? (If I explicitly set Password property to dummy value, error is gone.)

Edit: I've removed the code since it is lengthy and may be the cause of no answer yet. If somebody wants to have a look, I can append it below.


Automatic validation in EF is somehow strange feature - I don't like it. You can read this article to find some information how to validate just selected properties but I expect you must trigger that validation manually and turn off global validation by calling:

context.Configuration.ValidateOnSaveEnabled = false;

Your problem with NonMappedAttribute is interesting. I didn't go deep into implementation of validation in EFv4.1 but if the implementation is build around the same rules as common validation based on data annotations, it uses only attributes derived from ValidationAttribute - NotMappedAttribute is not derived from ValidationAttribute.

That is another problem of such implementation - it combines mapping definition and validation but these two features are not the same and should not be implemented by the same API.

@alun deleted his answer - the valid answer to your question. Your validation belongs to view model dependent on the operation a user is performing. It doesn't belong to persistence model. Why? Exactly because of your current issue - persistence model can hold only single validation set and every operation in your application must ensure that validation criteria for that set are met = you must ensure that Password and ConfirmPassword are filled even if your current operation doesn't demand it => problem.


Old thread but since I faced the exact same issue with EF6, please allow me to share my experience, in case someone else is searching the same thing.

Issue: Exact the same: Password & ConfirmPassword 2 [NotMapped] fields of User class. When trying to save New record, I was getting validation exception that both fields were required.

Investigation: I noticed that when I was updating the user record, I had no problem. Searching for what is the difference, I saw that I had overridden the Validate method

public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

for validating the MVC model, and the EF6 was displaying my custom error message! Voila! EF6 used also this Validate method! (and due to a bug in my code, it was triggering the error).

Conclusion: When saving an entity, EF6 will trigger the Validate method and any 'ValidationResult' will prevent record for saving.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜