Tuesday, August 26, 2014

DataAnnotation in models in edmx

In edmx model we have:
namespace DatabaseManager
{
    using System;
    using System.Collections.Generic;
   
    public partial class Item
    {

        public string IDescription { get; set; }
        public string IName { get; set; }

    }
}

If I set DataAnnotation like [Required] on property then it will not persist after edmx update. So it set annotation permanently we have to override meta information this class and merge our custom class with this class.
In our custom model:

namespace DatabaseManager
{
    [MetadataType(typeof(metaClass))]
    public partial class Item
    {

        private class metaClass
        {

            [DisplayName("Description")]
            public string IDescription { get; set; }

            [DisplayName("Name")]
            [Required()]
            public string IName { get; set; }

         }
     }
}

Now you set data annotation in your custom class it will work properly and no need to worry about edmx update.

No comments:

Post a Comment