将属性设置为类型为';System.Single';错误

本文关键字:Single 错误 System 属性 设置 类型 | 更新日期: 2023-09-27 18:27:28

在我的数据库中,数据类型是Float。在我的c#模型中,如果我给Decimal,我会得到错误

我的数据库表屏幕截图

 [1]: https://i.stack.imgur.com/RwGj1.png

错误消息

The 'IRR' property on 'DisburseCaseCommissionViewModel' could not be set to a 'System.Double' value. You must set this property to a non-null value of type 'System.Decimal'.

我的C#模式

 public class DisburseCaseCommissionViewModel
{
    [Key]
    public long Id { get; set; }
    public long CaseTFlexId { get; set; }
    public long? WorkRequestId { get; set; }
    public DateTime? ContractDate { get; set; }        
    public string CustomerName { get; set; }
    public decimal? CommissionPayoutAmount { get; set; }
    public string CommissionMonth { get; set; }
    public string PayOutType { get; set; }
    public Decimal? IRR { get; set; }
    public bool? IsDeleted { get; set; }
    public long? CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public long? LastModifiedBy { get; set; }
    public DateTime? LastModifiedDate { get; set; }
}

如果我给double,代码运行良好,但double会进行舍入,我不应该进行舍入。

将属性设置为类型为';System.Single';错误

您发现的是,您必须将C#类型与数据库中的实际数据类型相匹配。根据实际使用的数据库引擎,有各种各样的表可以匹配它们,所以我不在这里做了。

然而,假设Microsoft SQL。。。

SQL FLOAT数据类型是一个双精度浮点值,相当于C#中的double。如果需要Decimal,则在数据库结构中将其声明为DECIMAL()NUMERIC()

寓意是:设置数据库以按照您需要的方式进行操作,然后配置数据层以正确地与它对话。如果要存储decimal,请将字段设置为DECIMAL。如果需要FLOAT字段,则在代码中使用double