How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

前端 未结 15 907
-上瘾入骨i
-上瘾入骨i 2020-12-07 19:39

I\'m using the DbContext and Code First APIs introduced with Entity Framework 4.1.

The data model uses basic data types such as string

15条回答
  •  青春惊慌失措
    2020-12-07 20:11

    Simple. On your code first, set the type of DateTime to DateTime?. So you can work with nullable DateTime type in database. Entity example:

    public class Alarme
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int Id { get; set; }
    
            public DateTime? DataDisparado { get; set; }//.This allow you to work with nullable datetime in database.
            public DateTime? DataResolvido { get; set; }//.This allow you to work with nullable datetime in database.
            public long Latencia { get; set; }
    
            public bool Resolvido { get; set; }
    
            public int SensorId { get; set; }
            [ForeignKey("SensorId")]
            public virtual Sensor Sensor { get; set; }
        }
    

提交回复
热议问题