在哪里把数据注解的标签? [英] Where to put data annotations tags?

查看:86
本文介绍了在哪里把数据注解的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过亲asp.net mvc的准备2.0框架,似乎他穿上类,也产生了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; }

不过,我想知道如果我不开发我的数据库这种方式会发生什么。如果我只是添加到我的解决方案linqtosql.dbml(LINQ to SQL类)文件的形式,我得到了很好的设计师,会发生什么

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?

推荐答案

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

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

我用这个通过部分类附加属性上产生code属性。它的作品真的很好!

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

这篇关于在哪里把数据注解的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆