如何在Entry.state == EntityState.Added中设置自定义验证 [英] How to set custom validation just where Entry.state == EntityState.Added
问题描述
如何设置自定义验证,当 Entry.state
== EntityState.Added
是真的下面的代码将工作!
自定义验证码:
[AttributeUsage(AttributeTargets.Property,AllowMultiple = false)]
class UniqueEmailAddress :ValidationAttribute
{
protected override ValidationResult IsValid(object value,ValidationContext validationContext)
{
this.email =(string)value;
using(G9EntityDataBaseClass oEntity = new G9EntityDataBaseClass())
{
if(oEntity.User.Where(u => u.email == email).Count()== 0)
{
return ValidationResult.Success;
}
else
{
返回新的ValidationResult(ErrorMessageString);
}
}
}
}
使用:
[CustomValidation.UniqueEmailAddress]
public string email {set;得到;我需要((NOT)EntityState.Added):
$($)
if(Entry.state!= EntityState.Added){
return ValidationResult.Success;
}
从哪里我带来
Entry.state
解决方案一种适用于验证的方法是实现 IValidatableObject :
public class User:IValidatableObject
在
Validate
方法,您可以通过将实体对象添加到
ValidationContext
中来访问实体对象所附加的上下文。因此,您的验证可能如下所示:public IEnumerable&ValidationResult>验证(ValidationContext validationContext)
{
var context =(G9EntityDataBaseClass)validationContext.Items [Context];
if(context.User
.Where(u => u.email == email&&u.UserID!= this.UserID)
.Any())
{
返回新的ValidationResult(ErrorMessageString);
}
else
{
return ValidationResult.Success;
}
}
将上下文添加到验证上下文中,覆盖此方法在您的
DbContext
类:保护覆盖DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry ,
IDictionary< object,object> items)
{
items.Add(Context,this);
return base.ValidateEntity(entityEntry,items);
}
逻辑很简单:如果有相同的<$ c,验证总是失败$ c> email 属于另一个用户(
u.UserID!= this.UserID
)。这适用于插入新用户(ID = 0)和现有用户。
顺便提一下,它是有争议的,您是否应该实际使用这里的上下文访问数据库,但替代方法是覆盖
DbContext.ValidateEntity
不会吸引我,因为这会导致长时间的代码充满类型转换其中每次只需要一个。并且为验证的每个实体遍历该代码。验证
方法id仅在适用时执行。How to set custom validation that when
Entry.state
==EntityState.Added
is true the below code would work! Custom Validation Code:[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] class UniqueEmailAddress : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { this.email = (string)value; using (G9EntityDataBaseClass oEntity = new G9EntityDataBaseClass()) { if (oEntity.User.Where(u => u.email == email).Count() == 0) { return ValidationResult.Success; } else { return new ValidationResult(ErrorMessageString); } } } }
use :
[CustomValidation.UniqueEmailAddress] public string email { set; get; }
i need ((NOT)EntityState.Added):
if (Entry.state != EntityState.Added){ return ValidationResult.Success; }
From where should i bring
Entry.state
解决方案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 theValidationContext
. 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
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. TheValidate
method id only executed when its applicable.这篇关于如何在Entry.state == EntityState.Added中设置自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!