Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用EntityFramework在setter中进行验证_Asp.net_Entity Framework - Fatal编程技术网

Asp.net 使用EntityFramework在setter中进行验证

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

使用实体框架。我创建了一个对象注册,属性为“ammontofchildren”,最大值设置为50。我想在setter中进行验证,以确保最大弹药量为50。我该如何以最好的方式去做呢

  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时验证属性