A method that works well for validation is implementing IValidatableObject in your entity class:
public class User: IValidatableObject
In the Validate
method you can gain access to the context to which the entity object is attached at that moment by adding it to the ValidationContext
. So your validation could look like:
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var context = (G9EntityDataBaseClass)validationContext.Items["Context"];
if (context.User
.Where(u => u.email == email && u.UserID != this.UserID)
.Any())
{
return new ValidationResult(ErrorMessageString);
}
else
{
return ValidationResult.Success;
}
}
You add the context to the validation context by overriding this method in your DbContext
class:
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry,
IDictionary<object, object> items)
{
items.Add("Context", this);
return base.ValidateEntity(entityEntry, items);
}
The logic is simple: validation always fails if there is an identical email
that belongs to another user (u.UserID != this.UserID
). This applies both to inserting new users (ID = 0) and existing users.
By the way, it's debatable whether you should actually use the context here to access the database, but the alternative, overriding DbContext.ValidateEntity
doesn't appeal to me either, because this causes long strands of code full of type casts of which only one is necessary each time. And this code is traversed for each entity that's validated. The Validate
method id only executed when its applicable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…