在哪里放置数据注释标签?

cho*_*bo2 2 c# linq-to-sql asp.net-mvc-2

我正在使用专业的 asp.net mvc 2.0 框架,似乎他将他的数据注释标签放在也生成 linq 到 sql 的类上。

  [Table(Name = "Products")]
    public class Product
    {
        [HiddenInput(DisplayValue = false)]
        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int ProductID { get; set; }

        [Required(ErrorMessage = "Please enter a product name")]
        [Column] public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a description")]
        [DataType(DataType.MultilineText)]
        [Column] public string Description { get; set; }

        [Required]
        [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
        [Column] public decimal Price { get; set; }

        [Required(ErrorMessage = "Please specify a category")]
        [Column] public string Category { get; set; }

        [Column]
        public byte[] ImageData { get; set; }

        [ScaffoldColumn(false)] [Column]
        public string ImageMimeType { get; set; }
Run Code Online (Sandbox Code Playgroud)

但是我想知道如果我不以这种方式开发我的数据库会发生什么。如果我只是在我的解决方案中添加一个 linqtosql.dbml(linq to sql 类)文件,在那里我得到了那个漂亮的设计器,会发生什么。

我会把所有这些数据注释放在哪里,我会制作另一个包含所有这些内容的类吗?或者也许在视图模型中?

Kir*_*rst 5

您是否尝试过使用 MetadataType 属性?

public class IProductMetadata
{         
    [HiddenInput(DisplayValue = false)]
    int ProductID;

    [Required(ErrorMessage = "Please enter a product name")]         
    string Name;

    [Required(ErrorMessage = "Please enter a description")]         
    string Description;
    // etc
}

[MetadataType(typeof(IProductMetadata))]
public partial class Product
{
}
Run Code Online (Sandbox Code Playgroud)

我使用它通过部分类将属性附加到生成的代码上的属性。它真的很好用!