实体框架代码中的SQL'时间'类型首先 [英] SQL 'time' type in Entity Framework Code First

查看:77
本文介绍了实体框架代码中的SQL'时间'类型首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在一个表中创建一个时间(7)列,实体框架代码首先。这是我的实体:

I'm trying to create a 'time(7)' column in a table with Entity Framework Code First. This is my Entity:

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到时间,但是我收到这个错误:

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:


(112,12):错误2019:指定的成员映射不是有效。 ShiftDetail类型的成员StartTime的类型'Edm.DateTime [Nullable = False,DefaultValue =,Precision =]'与'SqlServer.time [Nullable = False,DefaultValue =,Precision = 7]]不兼容成员'StartTime'在类型'CodeFirstDatabaseSchema.ShiftDetail'。

(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'.

我也尝试过与TypeName =time(7)但我得到这个其他错误:

I've tried also with TypeName="time(7)" but I get this other error:


(104,6):错误0040:类型时间(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)

How can I create a time column with code first? (preferably without fluent API)

提前感谢

推荐答案

如果要使用 Time 键入数据库,您将不得不在应用程序中使用 TimeSpan 以24小时的周期。 DateTime 不表示时间。

If you want to use Time type in database you will have to use TimeSpan with 24 hour cycle in your application. DateTime is not representation of time.

这篇关于实体框架代码中的SQL'时间'类型首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆