我试图在Entity Framework Code First的表格中创建一个’time(7)’列.这是我的实体:
public class ShiftDetail { public long Id { get; set; } [required] public int DayOfWeek { get; set; } [required] [Column(TypeName="time")] public DateTime StartTime { get; set; } [required] [Column(TypeName = "time")] public DateTime EndTime { get; set; } public long ShiftId { get; set; } public virtual Shift Shift { get; set; } }
正如你所看到的,我试图将列的StartTime和EndTime的数据库类型设置为“time”,但是我得到这个错误:
(112,12) : error 2019: Member Mapping specified is not valid. The type ‘Edm.DateTime[Nullable=False,DefaultValue=,Precision=]’ of member ‘StartTime’ in type ‘ShiftDetail’ is not compatible with ‘sqlServer.time[Nullable=False,Precision=7]’ of member ‘StartTime’ in type ‘CodeFirstDatabaseSchema.ShiftDetail’.
我也试过TypeName =“time(7)”,但我得到这个其他的错误:
(104,6) : error 0040: The Type time(7) is not qualified with a namespace or alias. Only primitive types can be used without qualification.
如何创建一个时间列代码第一? (优选没有流利的API)
提前致谢.
解决方法
如果要在数据库中使用时间类型,则必须在应用程序中使用24小时循环的TimeSpan. DateTime不是时间的表示.