数据注释标签放在哪里?

发布于 2024-10-11 04:37:20 字数 1204 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姜生凉生 2024-10-18 04:37:20

您是否尝试过使用 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
{
}

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

Have you tried using the MetadataType attribute?

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
{
}

I use this to attach attributes to properties on generated code through the partial class. It works really well!

雪落纷纷 2024-10-18 04:37:20

我在视图模型上执行此操作,并使用 AutoMapper 在数据对象和视图模型之间进行映射。

I do it on the view model and use AutoMapper to map between data object and the view model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文