Entity Framework Code First Date field creation

后端 未结 8 2290
情歌与酒
情歌与酒 2020-11-30 23:13

I am using Entity Framework Code First method to create my database table. The following code creates a DATETIME column in the database, but I want to create a

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 00:10

    This is just an enhancement for the most up-voted answer by @LadislavMrnka on this question

    if you have a lot of Date columns, then you can create custom attribute and then use it when ever you want, this will produce more clean code in the Entity classes

    public class DateColumnAttribute : ColumnAttribute
    {
        public DateColumnAttribute()
        {
            TypeName = "date";
        }
    }
    

    Usage

    [DateColumn]
    public DateTime DateProperty { get; set; }
    

提交回复
热议问题