【发布时间】:2020-01-15 21:45:58
【问题描述】:
我有一个通过 Nuget 提供给我的课程。我没有来源。
public class SpecialProductResult
{
public int id { get; set; }
public decimal SpecialPercent {get;set;}
}
我想从存储过程中填充 SpecialProductResult 列表
所以在我的 DbContext 中我有
public DbQuery<SpecialProductDto> SpecialProducts { get; set; }
我使用
填充列表var specialProducts = connect.SpecialProducts.FromSql("spGetSpecialProducts").ToList()
在错误日志中我看到类似
的消息没有为实体上的小数列“SpecialPercent”指定类型 输入“SpecialProductResult”。这将导致值静默 如果它们不符合默认精度和比例,则被截断。 显式指定可以容纳所有的 SQL Server 列类型 使用“ForHasColumnType()”的值。
看了this question,想试试
modelBuilder.Entity<SpecialProductResult>().Property(o => o.GoldPercent).HasPrecision(18,4)
但是没有属性 .HasPrecision
我应该尝试什么?
[更新]
我尝试了 Ivan Stoev 的回答,但收到了运行时错误
The entity type 'SpecialProductResult' cannot be added to the model because a query type with the same name already exists
【问题讨论】:
-
GoldPercent 显然是错误的,不是吗?然后按照错误消息的提示,尝试 HasColumnType("decimal(18.4)") 而不是 HasPrecision() 。或者,您可以使用属性属性 [Column(TypeName = "decimal(18,4)")] 而不是使用 fluent api
标签: c# entity-framework-core entity-framework-core-2.1