[RESOLVED]DB First model.tt partial classes, can they be used for data annotations, etc?

I am using MVC4 with db first, so I created an EDMX folder which created a model.tt folder full of partial classes that represent my tables. I assume these are POCOs.  I had read that you dont want to make modifications to these classes because thery are
overwritten when you update the edmx model from the database.  Is this the case with these partial classes?  I want to use validation in my apop, but I can’t figure out where to put the data annotations. If not on these classes, maybe viewmodels?

Hi,

We better not to change any auto generate when we create or update .edmx. To make validation or data annotation, we can make partial classes on a different folder but the same nameSpace on each partial class. For example model name is Person.cs and a new
partial class also Person.cs both using the same name space but each on the different folder.  

Here the sample of auto generate using nameSpace "PersonEntity.Model" on root folder:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace PersonEntity.Model
{
    using System;
    using System.Collections.Generic;
    
    public partial class Person
    {
        public Person()
        {
            this.Attendances = new HashSet<Attendance>();
            this.Sessions = new HashSet<Session>();
            this.FactComments = new HashSet<FactComment>();
        }
    
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string NickName { get; set; }
        public Nullable<int> PersonTypeId { get; set; }
        public bool NameStyle { get; set; }
        public string Title { get; set; }
        public string Suffix { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Blog { get; set; }
        public string Twitter { get; set; }
        public string Gender { get; set; }
        public string ImageSource { get; set; }
        public string Bio { get; set; }
        public int PersonCategoryId { get; set; }
        public System.DateTime BirthDate { get; set; }
        public string MaritalStatus { get; set; }
        public System.DateTime DateAdd { get; set; }
        public string AdditionalContactInfo { get; set; }
        public string Demographics { get; set; }
        public System.Guid RowGuid { get; set; }
        public System.DateTime ModifiedDate { get; set; }
        public bool IsLocked { get; set; }
    
        public virtual ICollection<Attendance> Attendances { get; set; }
        public virtual ICollection<Session> Sessions { get; set; }
        public virtual ICollection<FactComment> FactComments { get; set; }
    }
}

Here a new Person.cs on using the same nameSpace "PersonEntity.Model" on "Partials" folder:

using System.ComponentModel.DataAnnotations;

namespace PersonEntity.Model
{
    [MetadataType(typeof(PersonMetadata))]
    public partial class Person
    {
        [Display(Name = "Full Name")]
        public string FullName
        {
            get
            {
                string fullName = string.Empty;
                if (NameStyle)
                    fullName = ((FirstName + " " + MiddleName).Trim() + " " + LastName).Trim();
                else
                {
                    fullName = LastName == string.Empty || LastName == null ? string.Empty : (LastName + " " + MiddleName).Trim();
                    fullName = fullName == string.Empty ? FirstName : (fullName + ", " + FirstName).Trim();
                }

                return fullName;
            }
        }

        public class PersonMetadata
        {
            [StringLength(25, ErrorMessage = "First name must be 25 characters or less in length.")]
            [Required(ErrorMessage = "First name is required.")]
            public string FirstName { get; set; }
        }
    }
}

Have fun

 

Data annotation should be applied to viewmodel, not domain model (entity). Moreover, you can use tools such as AutoMapper to convert viewmodel to domain model.

http://www.elevenwinds.com/data-validation-in-asp-net-mvc-database-first

you can follow the above link to get more clear knowledge on this. This article has explained in a nice manner.

Hope this will help.

Thanks

Leave a Reply