如何防止被截断至2位上保存使用的EntityFramework 4.1 CodeFirst十进制值? [英] How do I prevent decimal values from being truncated to 2 places on save using the EntityFramework 4.1 CodeFirst?

查看:189
本文介绍了如何防止被截断至2位上保存使用的EntityFramework 4.1 CodeFirst十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
实体框架代码优先:小数精度






我M使用LINQ到实体与4.1的EntityFramework代码优先系统。我所有的小数属性被截断在保存小数点后两位。我可以检查对象被修改,并且可以在这是有小数位的权数调试器看到和我很难在数据库代码的价值,以表明它可以接受小数位数的权数[在这种情况下,十进制(4,3)]。但是,当我保存的价值,它永远不会正确保存它,而是截断小数数值到小数点后两位。我一直没能找到一类 System.ComponentModel.DataAnnotations ,允许你指定的精度。类(消毒),供参考:

 公共类度量
{
公共小数? PPM {搞定;组; }
}


解决方案

 公共类MyContext:的DbContext 
{
公共DbSet<&度量GT;度量{搞定;组; }

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
modelBuilder.Entity<度量方式>()房产(X => x.PPM).HasPrecision(4, 3);
}
}


Possible Duplicate:
Entity Framework Code First: decimal precision

I'm using Linq-to-Entities with the EntityFramework 4.1 Code First system. All of my decimal properties are being truncated to two decimal places on save. I can examine the object being modified and can see in the debugger that is has the right number of decimal places and I can hard code the value in the database to show that it can accept the right number of decimal places [in this case, decimal(4,3)]. But when I save the value, it never saves it correctly, but instead truncates decimal values to two decimal places. I haven't been able to find a class in System.ComponentModel.DataAnnotations that allows you to specify precision. The class (sanitized), for reference is:

public class Metrics
{
    public decimal? PPM { get; set; }
}

解决方案

public class MyContext : DbContext
{
    public DbSet<Metrics> Metrics { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Metrics>().Property(x => x.PPM).HasPrecision(4, 3);
    }
}

这篇关于如何防止被截断至2位上保存使用的EntityFramework 4.1 CodeFirst十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆