Reputation: 129
I have a table with the following fields:
dbo.AccountProbability
StageKey (binary(16), not null)
AccountId (int, not null)
Probability (real, null)
IsCurrent (bit, not null)
It is mapped in Entity Framework like so:
[Table("dbo.AccountProbability")]
public partial class AccountProbability
{
[Required]
[MaxLength(16)]
public byte[] StageKey { get; set; }
public int AccountId { get; set; }
public double? Probability { get; set; }
public bool IsCurrent { get; set; }
}
When I try to map it to an object, in the below method, I get an error:
public async Task GetAccountProbabilities()
{
var repo = GetDatabaseRepo();
var validAcctProbs = repo.Where<AccountProbability>(
m => m.IsCurrent).ToList();
}
private static IDatabaseRepository GetDatabaseRepo()
{
var context =
new DbContext(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
return new DatabaseRepository(context);
}
It fails on validAcctProbs
when it's putting it to list, with the error: The 'Probability' property on 'AccountProbability' could not be set to a 'System.Single' value. You must set this property to a non-null value of type 'System.Double'.
I believe that reals in TSQL are doubles in EF.
EDIT: I do not believe this is a dupe because the previous question was specifically about SQLite and faulty mappings in that driver. This is for Microsoft TSQL.
Upvotes: 3
Views: 3826
Reputation: 8726
I believe that reals in TSQL are doubles in EF
Documentation is a bit ambiguous about this. We need to look it up in the implementation. The source code of EF6 is public, so we find:
<Type Name="tinyint" PrimitiveTypeKind="Byte"></Type>
<Type Name="smallint" PrimitiveTypeKind="Int16"></Type>
<Type Name="int" PrimitiveTypeKind="Int32"></Type>
<Type Name="bigint" PrimitiveTypeKind="Int64"></Type>
<Type Name="float" PrimitiveTypeKind="Double"></Type>
<Type Name="real" PrimitiveTypeKind="Single"></Type>
<Type Name="decimal" PrimitiveTypeKind="Decimal">
Let me show why this makes sense:
float
is a 4 byte (32 bit) floating point number.Range of real
: -3.40e38 to 3.40e38
Range of Single
: -3.402823e38 to +3.402823e38
So there is no point using the double?
type for your Probability
field, because the real
will never exhaust the precision of a Single
.
Upvotes: 3