My code is like this.
public class MyDbContext : DbContext
{
public virtual DbSet<CurrentData> CurrentData { get; set; }
}
public class CurrentData
{
public int ID { get; set; }
public DateTime Time { get; set; }
public float Value { get; set; }
}
...
float f = 1.37E-40f;
using (var context = new MyDbContext())
{
var data = new CurrentData() { ID = 100, Time = DateTime.Now, Value = f };
context.CurrentData.Add(data);
var result = context.SaveChanges();
}
It raises exception on SaveChanges()
because of the small float value.
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 4 (\"@1\"): The supplied value is not a valid instance of data type real. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.
If I change f
to 0, it works properly.
When I checked SQL Server's REAL
type range, the minimum positive value is 1.18E-38.
https://msdn.microsoft.com/en-us/library/ms173773.aspx
So if float value in C# is smaller than 1.18E-38, the exception occurs.
I changed f
value like this
if (f > 0 && f < 1.18E-38f)
f = 0;
else if (f < 0 && f > -1.18E-38f)
f = 0;
But I think it doesn't look good.
What will be better solution?
I tested using SqlDataAdapter
instead of Entity Framework, and got same exception.
I used EF v6.1.3 and .NET 4.5.1 with SQL Server 2012.