views:

59

answers:

1

I would like to be able to use DataAnnotations when working with my model, so for instance be able to write stuff like

[DisplayName("Title")]
[StringLength(256)]
public string title { get; set; }

I also want to be able to use the column with linq which has led me so far to adding table and column linq mapping annotations such as

[Column(DbType = "NVarChar(256)", UpdateCheck = UpdateCheck.Never)]

However this seems to be a long winded solution.

I would like to be able to use the auto generated linqtosql class but there doesn't seem to be a clear way to use this in conjunction with data annotations.

From research I have done I have come to the conclusion it is not possible to add the annotations using partial classes, and I don't want to have to create another class just for the annotations and start worrying about mapping the auto generated class manually. I also know it is bad practice to edit the code file of the auto generated class manually.

Is there a nice solution to this?

+2  A: 

The best way I found was this http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs

namespace MvcApplication1.Models
{
    [MetadataType(typeof(MovieMetaData))]
    public partial class Movie
    {
    }


    public class MovieMetaData
    {
        [Required]
        public object Title { get; set; }