数据注释标签放在哪里?
我正在使用 pro asp.net mvc 2.0 框架,似乎他将数据注释标记放在也生成 linq to 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; }
但是我想知道如果我不以这种方式开发数据库会发生什么。如果我只是将 linqtosql.dbml (linq to sql 类)文件添加到我的解决方案中,我会得到那个漂亮的设计器,会发生什么。
我将把所有这些数据注释放在哪里,我会创建另一个包含所有这些内容的类吗?或者也许在视图模型中?
I am going through pro asp.net mvc 2.0 framework and it seems that he puts his data annotation tags on classes that also generate the linq to 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; }
However I am wondering what happens if I don't develop my database this way. What happens if I just add to my solution a linqtosql.dbml ( linq to sql class) file where I get that nice designer.
Where would I put all these data annotations would I make another class what would have all this content in? Or maybe in the view models?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过使用 MetadataType 属性?
我使用它通过分部类将属性附加到生成代码的属性。效果真的很好!
Have you tried using the MetadataType attribute?
I use this to attach attributes to properties on generated code through the partial class. It works really well!
我在视图模型上执行此操作,并使用 AutoMapper 在数据对象和视图模型之间进行映射。
I do it on the view model and use AutoMapper to map between data object and the view model.