How to set custom validation just where Entry.state == EntityState.Added

前端 未结 3 2141
花落未央
花落未央 2020-12-12 03:42

How to set custom validation that when Entry.state == EntityState.Added is true the below code would work! Custom Validation Code:

         


        
3条回答
  •  余生分开走
    2020-12-12 03:44

    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 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 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.

提交回复
热议问题