0

System.ComponentModel.DataAnnotations在我的模型中使用了各种语法。想知道使用这种简写语法是否对放置有任何影响,DataAnnotations以及下面进一步看到的放置DataAnnotations在 Attributes 而不是 Accessor & Modifier 方法上的位置。

我的模型具有我认为是最佳实践的放置方式,DataAnnotations当我将 OData 支持添加到未在此处发布的数据库上下文类时,它可以按预期工作

public class WebOrder
{
    private Guid _id;
    private float _total;
    private string _name;         

    [Key]
    public Guid Id { get; private set; }

    [Required]
    public float Total
    {
        get { return _total; }
        set { _total = value; }
    }        

    [Required]
    public string Name { get => _name; set => _name = value; }
}

这个位置是否DataAnnotations也被认为是有效的

public class WebOrder
{
    [Key]
    private Guid _id;
    [Required]     
    private float _total;
    [Required]    
    private string _name;         

    public Guid Id { get; private set; }

    public float Total
    {
        get { return _total; }
        set { _total = value; }
    }        

    public string Name { get => _name; set => _name = value; }
}
4

0 回答 0