Sunday, 15 April 2012

Entity Framework Code First truncating my decimals -



Entity Framework Code First truncating my decimals -

i using entity framework 6.x using code first approach on mvc 5 application. in particular situation model (among other things) contains 2 properties named latitude , longitude:

[required, range(-90, +90)] public decimal latitude { get; set; } [required, range(-180, +180)] public decimal longitude { get; set; }

and when performed migration got this

createtable("resproperty"), c => new { : latitude = c.decimal(nullable: false, precision: 10, scale: 8), longitude = c.decimal(nullable: false, precision: 11, scale: 8), : }) ... other stuff

so both latitude , longitude have 8 decimal digits. former 2 whole numbers (max 90) , latter 3 whole numbers (max 180).

after performing update-database command table's columns shown as:

latitude decimal(10,8) longitude decimal(11,8)

that seems me. in view have map , javascript code allows user reposition marker. works fine too. when marker repositioned latitude , longitude fields populated updated value (javascript) has more 12 decimal digits. not matter afaik because scale 8 decimals.

after submit button pressed , either create or edit post method invoked examine model instance , confirmed actual values passed in model controller correct, have more plenty decimal digits (those javascript code place). value correct.

now... problem beingness after db.savechanges() performed database gets updated -and have confirmed actual write/update has taken place- somehow internally ef disregards actual values , writes truncated latitude/longitude rounded 2 decimal digits, latitude shows in db 09.500000000 other decimal digits zeroed because rounding seems have taken place.

// prior savechanges() latitude = 9.08521879 longitude = -79.51658792 // after savechanges() latitude = 9.08000000 longitude = -79.51000000

why rounding if have given right scale , precision , column has right scale , precision well? why savechanges altering values?

i found post (http://weiding331.blogspot.com/2014/01/entity-framework-decimal-value.html) same issue don't know how can prepare (if does) because have performed several migrations , info additions after table in question "migrated".

summarizing

the model info type right (decimal) the database migration code has right precion/scale (lat 10/8 lon 11/8) the sql database columns have right precision/scale (lat 10/8, long 11/8) the values passed in model have @ to the lowest degree 8 decimal digits both latitude , longitude the actual writing/updating of value takes place in database without error, but... the values recorded on database these 2 columns truncated 2 decimal digits , show other to the lowest degree important decimal digits 0 (0)

ef-code-first decimal entity-framework-6 geospatial rounding-error

No comments:

Post a Comment