.net Entity Framework Code First 中的 SQL 'time' 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12186044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL 'time' type in Entity Framework Code First
提问by Escobar5
I'm trying to create a 'time(7)' column in a table with Entity Framework Code First. This is my Entity:
我正在尝试使用 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; }
}
As you can see I'm trying to set the database type for the columns StartTime and EndTime to "time" but I get this error:
如您所见,我正在尝试将 StartTime 和 EndTime 列的数据库类型设置为“时间”,但出现此错误:
(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,DefaultValue=,Precision=7]' of member 'StartTime' in type 'CodeFirstDatabaseSchema.ShiftDetail'.
(112,12) :错误 2019:指定的成员映射无效。'ShiftDetail' 类型中成员 'StartTime' 的类型 'Edm.DateTime[Nullable=False,DefaultValue=,Precision=]' 与 'SqlServer.time[Nullable=False,DefaultValue=,Precision=7]' 不兼容“CodeFirstDatabaseSchema.ShiftDetail”类型中的成员“StartTime”。
I've tried also with TypeName="time(7)" but I get this other error:
我也试过 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.
(104,6) : 错误 0040: 类型 time(7) 没有用命名空间或别名限定。只有原始类型可以不加限定地使用。
How can I create a time column with code first? (preferably without fluent API)
如何首先使用代码创建时间列?(最好没有流畅的 API)
Thanks in advance.
提前致谢。
回答by Ladislav Mrnka
If you want to use Timetype in database you will have to use TimeSpanwith 24 hour cycle in your application. DateTimeis not representation of time.
如果要Time在数据库中使用类型,则必须TimeSpan在应用程序中使用24 小时周期。DateTime不是时间的表示。

