Asp.net 使用EntityFramework在setter中进行验证
使用实体框架。我创建了一个对象注册,属性为“ammontofchildren”,最大值设置为50。我想在setter中进行验证,以确保最大弹药量为50。我该如何以最好的方式去做呢Asp.net 使用EntityFramework在setter中进行验证,asp.net,entity-framework,Asp.net,Entity Framework,使用实体框架。我创建了一个对象注册,属性为“ammontofchildren”,最大值设置为50。我想在setter中进行验证,以确保最大弹药量为50。我该如何以最好的方式去做呢 public global::System.Int16 AmmountOfChildren { get { return _AmmountOfChildren; } set { O
public global::System.Int16 AmmountOfChildren
{
get
{
return _AmmountOfChildren;
}
set
{
OnAmmountOfChildrenChanging(value);
ReportPropertyChanging("AmmountOfChildren");
_AmmountOfChildren = StructuralObject.SetValidValue(value);
ReportPropertyChanged("AmmountOfChildren");
OnAmmountOfChildrenChanged();
}
}
最简单的方法是在属性上放置System.ComponentModel.DataAnnotations.RangeAttribute:
[Range(0, 50)]
public global::System.Int16 AmmountOfChildren
这将导致在调用SaveChanges时验证属性