.net - SQL 'time' type in Entity Framework Code First

标签 .net entity-framework entity-framework-4 ef-code-first code-first

我正在尝试使用 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 列的数据库类型设置为“时间”,但出现此错误:

(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)"但我得到了另一个错误:

(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)

提前致谢。

最佳答案

如果您想使用 Time输入您必须使用的数据库 TimeSpan在您的应用程序中以 24 小时为周期。 DateTime不是时间的表示。

关于.net - SQL 'time' type in Entity Framework Code First,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12186044/

相关文章:

c# - Entity Framework - 生成额外 JOIN 的导航属性

c# - TextBox 上的 AcceptsReturn 无法正常运行

c# - 在 .NET 2.0 中使用扩展方法?

entity-framework-4 - 使用 C# 和 Entity Framework CTP 4 的领域建模问题

asp.net - Entity Framework 第一次加载非常慢

entity-framework - EF Fluent 中的 Reference( Fluent nhibernate) 等效项是什么?

c# - 选择性地统一解决同一接口(interface)的依赖关系

c# - Fiddler 使 HttpWebRequest/HttpClient 行为意外

c# - 不支持关键字 : 'data source' in ASP.net Entity Framework 6

c# - 预加载包括使用 UseLazyLoadingProxies