Where to place data annotation tags?

I am looking through the framework of asp.net mvc 2.0 and it seems like it is putting its annotation tags on the classes that also generate linq for 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 will happen if I do not design my database in this way. What happens if I just add to my solution the linqtosql.dbml (linq to sql class) file where I get this beautiful designer.

Where would I put all these data annotations, would I make another class in which all this content will be? Or maybe in viewing models?

+2
source share
2 answers

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

, . !

+4

AutoMapper .

0

All Articles