【问题标题】:Defining precision for numeric data type of Postgres DB in Entity Framework Core在 Entity Framework Core 中为 Postgres DB 的数字数据类型定义精度
【发布时间】:2017-10-25 16:19:15
【问题描述】:

我正在使用带有 Entity Framework Core 的 Postgres 数据库。

在我的数据库中,Price 表有 price(numeric) 列,并且存储了诸如 12345.123456789012345678901234567890 之类的值,即(高精度)

当我使用 LINQ 查询获取数据时,每次我得到错误:

"System.OverflowException: 数值不适合 System.Decimal"

我在 DbContext 的 OnModelCreating() 方法中找到了将 Property.HasColumnType("decimal(20,5)") 用于数据库列的方法,但它不起作用。另外,我看到在 edmx 文件中定义选择查询,但 Entity Framework Core 不生成 edmx 文件。

那么,我们如何在 C# 中使用 Entity Framework Core 为 Postgres 的数值数据类型定义精度? (注:精度可以减少到小数点后至少5位)

【问题讨论】:

  • 你的 linq 查询是什么
  • 什么是模型属性价格数据类型?
  • linq 查询:dBContext.PriceTable.Where(x => x.id == 2).First().Value;
  • 价格数据类型为数字
  • numeric 是 sql DataType ,什么是 Frontend dataType decimal ,int ,float, and double

标签: c# postgresql linq entity-framework-core


【解决方案1】:

Decimal 不支持高精度,所以你使用了 Double 其他明智的价格值转换为十进制值

例子:

1.储值 价格=1234.12345678901234567890

tabledetails.Select(c =>(decimal) c.value).ToList();

获取 Vlaue 为 1234.12345678901

2.Other wise 之前使用十进制存储价格值 then 使用以下格式

存储价值

1234.12345678901234567890米

获取价值

1234.12345678901234567890

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2018-12-19
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多