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

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

问题描述

我想与实体框架$ C C首先$表中创建一个时间(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; }
}

正如你可以看到我想要设置的数据库类型的列的开始时间和结束时间为时间,但我得到这个错误:

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:​​指定的成员映射无效。类型类型Edm.DateTime [可空=假,默认值=,precision ='成员'开始时间'ShiftDetail'是不符合SqlServer.time [可空=假,默认值=,$ P $兼容pcision = 7]开始时间中输入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'.

我也试着用类型名=时间(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.

如何创建与code第一时间列? (preferably不流畅API)

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

在此先感谢。

推荐答案

如果你想使用时间输入数据库中,你将不得不使用时间跨度 24小时循环在你的应用程序。 DATATIME 不重的时候presentation。

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

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

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