There is a method to set format in Code First Migration
i.e:
..
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:d}")]
public Nullable<System.DateTime> DeliveredDate { get; set; }
..
However, I could achieve same logic in Database First Migration, since every change is the database is updating the models.
So, my question is: How to set DataAnnotations like (DisplayFormat, DataFormatString so on.) in Database first migration? Is is possible? If possible, how to implement it.
You can utilize the fact that the generated entity classes are partial and associate metadata though another class and MetadataTypeAttribute.
E.g., in some code file not affected by the code generation, you would write something like this:
[MetadataType(typeof(YourEntityMetadata))]
partial class YourEntity { }
class YourEntityMetadata
{
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:d}")]
public Nullable<System.DateTime> DeliveredDate { get; set; }
}
The "metadata" class does not need to include all properties of the entity - just the ones you want to associate data annotations with.
Reference: EF Database First with ASP.NET MVC: Enhancing Data Validation