Using Entity Framework. I have created an object Registration with a property “AmmountOfChildren” with max set to 50. I want to validate in the setter to make sure that the max ammount is 50. How do i go about it in the best way?
public global::System.Int16 AmmountOfChildren
{
get
{
return _AmmountOfChildren;
}
set
{
OnAmmountOfChildrenChanging(value);
ReportPropertyChanging("AmmountOfChildren");
_AmmountOfChildren = StructuralObject.SetValidValue(value);
ReportPropertyChanged("AmmountOfChildren");
OnAmmountOfChildrenChanged();
}
}
The easiest way would be to put a System.ComponentModel.DataAnnotations.RangeAttribute on the property:
This will cause the property to be validated when SaveChanges is called.