This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello: Background Info I've added a ValidateRange attribute to the person entity allowing 0 to 110. To test it I am attempting to create a new person at an existing address with an age of -1. The object [Gates, Bill (-1)] could not be save because it is invalid. The errors are: Age must be between 0 and 110 Is it possible to validate the person enity prior to the SaveChanges and receive just Age must be between 0 and 110? Ideally, if this happens I would like to be to identify exactly which field is invalid, is that possible? Matthew |
|
|
At the risk of answering my own question, I guess I can just further override the OnValidate or OnValidating methods?
|
|
|
After some more reading, I realized the issue. I have been using Entity.Validate() but this function isn't throwing an exception or doing really anything to let me know that the person is invalid. Is this the intended behavior? Checking Entity.IsValid works and then going through the Entity.Errors collections yields two ValidationErrors: Age must be between 0 and 110 ( Awesome exactly what I want ) Matthew |
|
|
Hi Matthew, The Address is invalid because it is associated with the invalid Person entity, so it is due to that association it is marked as invalid also.
Jeremy |
|
|
Hi Jeremy: If my Person entity had additional associations, i.e. an association to a Job entity, would that be reported as invalid as well? i.e If validation on any entity fails are all of its associations deemed invalid? I guess I don't understand why during the validation of the Person entity ( in an invalid state ) would report that it's Address association is invalid although the Address entity is valid ( after all, it's already in the database ). I would expect to see this error during the Address entity validation where it would report that its association to a Person entity is invalid because the Person is invalid. Really the issue is trouble with the Age of the Person entity, but two ( or more depending on the number of associations Person has ) errors are generated for it. In short what I am trying to accomplish is to utilize the validation mechanisms available to report errors back to the user. However it appears that during the creation of a Person entity, if the user selects an existing address, enters an invalid age, they will receive errors stating that Age and Address are invalid when really their Address selection could be completely acceptable.
Matthew |
|
|
Hello Matthew, I personally also find this confusing. One way to tackle this is to create extension methods for "local" validity and errors. You can determine if a validation error is actually an "associated object is invalid" error by looking at the ValidationError.AssociatedEntity property. This means you can write extension methods on Entity such as the following: public static bool IsValid(this Entity entity) { The extension methods will consider only "local" errors on the entity itself. Thus, in your case, person.Errors() would return only the Age error, and person.Address.IsValid() would return true. Hope this helps at least as an interim measure. |
|
|
Hi Jeremy and Ivan: Thanks for the great support. This solution will do fine in the interim. Matthew |
|