entity-framework – 首先阻止Entity Framework数据库中的十进制舍入

前端之家收集整理的这篇文章主要介绍了entity-framework – 首先阻止Entity Framework数据库中的十进制舍入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用c#,实体框架 – 数据库优先和mvc来编写/读取ms sql数据库的纬度和经度.

问题是lat / long只显示2位小数,即使它们在数据库中是正确的.数据库的字段为数字(18,6).

修改了上下文文件,如下所示:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<STORE_LOCATION>().Property(location => location.LATITUDE).HasPrecision(18,6);
        modelBuilder.Entity<STORE_LOCATION>().Property(location => location.LONGITUDE).HasPrecision(18,6);
    }

2个问题 – 我是否应该修改上下文文件中的OnModelCreating – 即使它警告更改可能会在重新生成时丢失,如何保持代码不会舍入小数?

任何帮助赞赏!

解决方法

Should I be modifying the OnModelCreating in the context file – even though it warns changes may be lost on regeneration and how do I keep the code from rounding the decimals?

否 – 在另一个文件中实现上下文的部分类,并在那里实现OnModelCreating

至于精度 – 您是否检查过调试器中对象的值与页面显示的值?在渲染期间可能发生舍入.

尝试将DisplayFormat属性添加到实体类中的属性

[DisplayFormat(DataFormatString = "{0:0.0#####}",ApplyFormatInEditMode = true)]
原文链接:https://www.f2er.com/mssql/76899.html

猜你在找的MsSQL相关文章