You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EntityFramework6.Npgsql如何读取time without time zone类型列

解决EF6 + Npgsql映射PostgreSQL time列的类型不匹配问题

错误原因明确:Npgsql在EF6中默认将.NET TimeSpan映射为PostgreSQL的interval类型,但你的数据库列是time without time zone,两者类型不兼容,导致查询时出现UNION types interval and time without time zone cannot be matched错误。

你可以通过以下两种方式显式配置属性的数据库类型来解决:

方式一:使用数据注解

直接在属性上通过[Column]注解指定数据库类型:

[Column("time_column", TypeName = "time without time zone")]
public TimeSpan Time { get; set; }

方式二:使用Fluent API

在你的DbContext的OnModelCreating方法中配置实体属性:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<你的实体类名>()
        .Property(e => e.Time)
        .HasColumnName("time_column")
        .HasColumnType("time without time zone");
}

两种方式本质都是告诉EF6:这个属性对应的数据库列是time without time zone类型,而非默认的interval,从而避免类型不匹配的错误。

内容的提问来源于stack exchange,提问作者wekso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 02:15:58