My database field (sql server 2005) is defined with numeric(15,2).
The LINQ 2 SQL generated property is
[Column(Storage="_My_Property_Name", DbType="Decimal(15,2)", UpdateCheck=UpdateCheck.Never)]
public System.Nullable<decimal> My_Property_Name
{
get
{
return this._My_Property_Name;
}
set
{
if ((this._My_Property_Name != value))
{
this.OnMy_Property_NameChanging(value);
this.SendPropertyChanging();
this._My_Property_Name = value;
this.SendPropertyChanged("My_Property_Name");
this.OnMy_Property_NameChanged();
}
}
}
In debug I check the entity value for this property = 23.6363636363 (etc)
I then step over context.SubmitChanges()
I have SQL Profiler running and this is the update statement.
exec sp_executesql N'
UPDATE [Staging].[My_Table_Name]
SET [LAST_UPDATE_DATE] = @p2, [Field1] = @p3, [Field2] = @p4, [Field3] = @p5, [Field4] = @p6, [Field5] = @p7, [Field6] = @p8, [Field7] = @p9
WHERE ([Id] = @p0) AND ([RecordVersion] = @p1)
SELECT [t1].[RecordVersion] FROM [Staging].[My_Table_Name] AS [t1]
WHERE ((@@ROWCOUNT) > 0) AND ([t1].[Id] = @p10)',N'@p0 int,@p1 timestamp,@p2 datetime,@p3 decimal(21,8),@p4 decimal(21,8),@p5 decimal(21,8),@p6 decimal(21,8), @p7 decimal(21,8),@p8 decimal(21,8),@p9 decimal(15,2),@p10 int',@p0=2935,@p1=0x0000000000323018,@p2='2010-02-26 16:49:21:690', @p3=99.99992307,@p4=99.99992307,@p5=99.99992307,@p6=99.99992307,@p7=99.99992307,@p8=99.99992307,
@p9=23.63,@p10=2935
As you can see @p9 = 23.63, I would expect it to be 23.64.
Update
My question is,
If this is a LINQ to SQL bug I would expect it to be a known one, where would I find this out; is there a maintained bug list somewhere?
Also what would be the best work around?
- I'm guessing changing the field to 15,3 wouldn't fix the bug, it would just shift it 1 decimal place.
- Overriding the OnMy_Property_NameChanged() would work for this property, but I have lots of them.
Update 2
this didn't work either, it goes into this piece of code before submitchanges and appears to work, but the generated update sql still has the truncated value, not this updated rounded value.
partial void OnMy_Property_Name_ChangingChanging(decimal? value)
{
if (!value.HasValue)
{
return;
}
value =
Math.Round(value.Value, 2, MidpointRounding.AwayFromZero);
}
The fix I've got at the moment is just to update the entity value directly.