Prevent decimal rounding in Entity Framework database first

I am attempting to write/read latitude and longitude to a ms sql database using c#, entity framework - database first, and mvc.

The problem is that the lat/long are being shown with only 2 decimal places even though they are correct in the database. The database has the fields as numeric(18,6).

I have modified the context file as follows:

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 Questions - 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?

Any help appreciated!


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?

No - implement a partial class of your context in another file and implement there OnModelCreating

As for the precision - have you checked the values in the objects in the debugger vs. displaying on a page? The rounding may be occurring during rendering.

Try adding the DisplayFormat attribute to the property in the entity class:

[DisplayFormat(DataFormatString = "{0:0.0#####}", ApplyFormatInEditMode = true)]
链接地址: http://www.djcxy.com/p/70494.html

上一篇: 占位符不适用于Internet Explorer

下一篇: 首先防止实体框架数据库中的小数四舍五入