0
votes

I'm using MVC 5 along with Entity Framework (6.0.2), one of my classes was generated as:

public partial class Proc_Result
{
    public int Id { get; set; }
    public string RegistrationNumber { get; set; }
    public string Name { get; set; }
    public Nullable<System.DateTime> DateOfEntry { get; set; }
    public string Drawer { get; set; }
}

I wanted to have it so that the DateOfEntry had a Data Annotation of: [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

Along with being able to add [DisplayName(..)] to the other fields, is this possible? I know I can manually change the generated cs file, but then whenever I update from the db I will loose those changes. Otherwise I could build my own model and map that, but wanted to see if there is another way.

I thought about creating another partial class called Proc_Result, but if I just include the same DateOfEntry, will it override the other one?

1

1 Answers

1
votes

I found the solution here: http://ryanhayes.net/blog/data-annotations-for-entity-framework-4-entities-as-an-mvc-model/ basically i had to make this:

    [MetadataType(typeof(Proc_ResultMetaData))]
public partial class Proc_Result
{
    // Note this class has nothing in it.  It's just here to add the class-level attribute.
}

public partial class Proc_ResultMetaData
{
    public int Id { get; set; }
    [DisplayName("Registeration Number")]
    public string RegistrationNumber { get; set; }
    public string Name { get; set; }

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    public Nullable<System.DateTime> DateOfEntry { get; set; }
    public string Drawer { get; set; }
}

That got it working.