Storing TimeSpan with Entity Framework Codefirst - SqlDbType.Time overflow

后端 未结 4 790
攒了一身酷
攒了一身酷 2021-02-05 02:41

I\'m trying to seed some constants into my DB:

context.Stages.AddOrUpdate(s => s.Name,
                                   new Stage()
                                


        
4条回答
  •  -上瘾入骨i
    2021-02-05 03:17

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property '" + nameof(Duration) + "' should be used instead.")]        
        public long DurationTicks { get; set; }
    
        [NotMapped]
        public TimeSpan Duration
        {
    #pragma warning disable 618
          get { return new TimeSpan(DurationTicks); }
          set { DurationTicks = value.Ticks; }
    #pragma warning restore 618
        }
    

    Update

    This is now achievable since EF Core 2.1, using Value Conversion.

    builder.Entity()
        .Property(s => s.Span)
        .HasConversion(new TimeSpanToTicksConverter()); // or TimeSpanToStringConverter
    

提交回复
热议问题