为什么EF核心代码首先生成DateTime2而不是DateTime?

时间:2019-05-20 07:03:31

标签: entity-framework ef-code-first

首先我是EF核心代码中的新手,我想成为datetime形式的变量,但要使用Datetime2。我该如何解决?

这是EF核心,代码优先方法和sql server 2017

public class MB_Message
{
    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int MessageID { get; set; }
    public DateTime? expireDateTime { get; set; }
    public string TypeName { get; set; }
    public bool IsForceLastSend { get; set; }
    public DateTime MessageDateTime { get; set; }
    public bool ReadOnce { get; set; }
    public string ExeCode { get; set; }
    [ForeignKey("MB_Queue")]
    public int QueueCode { get; set; }
    public int? UserID { get; set; }
    [ForeignKey("MB_MessageBody")]
    public int BodyID { get; set; }
    public bool Enable { get; set; }

}

1 个答案:

答案 0 :(得分:0)

在您的dbcontext的OnModelCreating方法中,添加以下内容:

  protected override void OnModelCreating(ModelBuilder builder)
    {

        builder.Entity<MB_Message>()
            .Property(x=> x.MessageDateTime )
            .HasColumnType("datetime");
    }