【发布时间】:2020-02-11 16:15:22
【问题描述】:
我在产品实体中有一个小数属性:
public class Product
{
public Guid Id { get; set; }
public decimal Price { get; set; }
}
我想在模型映射中配置精度:
class Context : DbContext
{
public DbSet<Product> Products { get; set; }
public Context() : base("server=localhost;database=myDb2;trusted_connection=true;")
{
/* Database.Delete();
Database.Create(); */
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().HasKey(e => new { e.Id });
modelBuilder.Entity<Product>().Property(s => s.Price).HasPrecision(29, 10);
}
}
现在我正在尝试保存长度为 19 的小数:
using (Context context = new Context())
{
var product = new Product();
product.Id = Guid.NewGuid();
product.Price = 9999999999999999999M;
context.Products.Add(product);
context.SaveChanges();
}
抛出异常:
System.Data.Entity.Infrastructure.DbUpdateException H结果=0x80131501 Message=更新条目时发生错误。有关详细信息,请参阅内部异常。 来源=实体框架 堆栈跟踪: 在 System.Data.Entity.Internal.InternalContext.SaveChanges() 在 System.Data.Entity.Internal.LazyInternalContext.SaveChanges() 在 System.Data.Entity.DbContext.SaveChanges() 在 C:\Users\dilshodk\source\repos\ConsoleApp11\ConsoleApp11\Program.cs:line 22 中的 ConsoleApp11.Program.Main(String[] args) 处
内部异常 1: UpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常。
内部异常 2: OverflowException:转换溢出。
当我尝试从 SQL 查询中插入值时,它可以工作:
Insert into Products values (NEWID(),9999999999999999999)
为什么它在 EF 中不起作用,我该如何解决?
【问题讨论】:
-
SQL 中
Price的类型是什么? -
@SelimYıldız 是十进制 (29,10)
-
@Sajid 它没有帮助
标签: c# entity-framework decimal