1

I have a record which contain a number in certain field: 25.99. Whenever I select this record using Devart DotConnect from my C# code it returns me 25.990000000000002. Therefore, my update statement prompt me such exception: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

My code snippet is stated as follow:

var busRoutes = (
    from route in ctx.RMBUSROUTEs
    select route
).ToArray();

foreach (var busRoute in busRoutes)
{
    busRoute.LASTUPDDATE = DateTime.Now;
}

ctx.SaveChanges();

There are ~ only 100 records inside this table. Most of the records contains decimal number in that field while only a specific certain record always retrieve 25.99 as 25.990000000000002. I am sure that the record value stored in DB is 25.99. How come? Thanks in advance.

mannok
  • 1,712
  • 1
  • 20
  • 30
  • what data type is it? because if it is float or double (which i would bet on), you better re-read on the properties of floating point numbers. the TL;DR is: never expect them to be precise, because they aren't. – Franz Gleichmann Jun 15 '21 at 10:56
  • @Franz Gleichmann programming type is double and db type is number(5, 2). This field is my primary key, if they aren't precise then how can I update records in this table? EF can never help me to match record then... – mannok Jun 15 '21 at 11:07
  • 2
    double as primary key is a horrible idea. you should never do that. especially since floating point numbers aren't precise. either use a synthetic ID (int or guid, in a pinch string), or at least change it to decimal. – Franz Gleichmann Jun 15 '21 at 11:53
  • 1
    (also, on the assumption that `25.99` is a bus route number or something: here, `string` is actually the weapon of choice. numeric data types are for numbers - if it doesn't make sense to, for example, _add_ two values, it's not a numeric value - even if it consists of digits.) – Franz Gleichmann Jun 15 '21 at 12:13
  • 1
    As @FranzGleichmann said, it is better not to use double as a primary key. The provider-specific conversions are described at https://forums.devart.com/viewtopic.php?t=16114#p175435. – Devart Jun 16 '21 at 14:23
  • 1
    Thanks @FranzGleichmann and Devart . For those who come into this question and mutating your table schema is not a solution, you may try to change the mapping entity from float/double to decimal. You may find how decimal resolve this problem from https://www.youtube.com/watch?v=PZRI1IfStY0 – mannok Jun 16 '21 at 17:18
  • @mannok maybe you could write an answer yourself with your solution, so whoever searches for the same problem in the future can find it more easily? (plus: i also like the https://floating-point-gui.de/ ) – Franz Gleichmann Jun 16 '21 at 18:37

1 Answers1

-2

Try to change type of LASTUPDDATE from DateTime to string. I dont know why, but it worked for me.

  • 1
    a) this answer has very low quality overall. b) it does not address the problem described in the question. c) answers based on "i don't know why" are never a good idea. we're not here to blindly guess, but to provide knowledge. please read [how to write a good answer](https://stackoverflow.com/help/how-to-answer) – Franz Gleichmann Jun 16 '21 at 14:32