Entity Framework: Mapping a db real to decimal? Cannot convert System.Single to System.Double

c# entity-framework entity-framework-6

Question

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.

1
3
5/24/2017 7:56:27 PM

Accepted Answer

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:

  1. In T-SQL, starting from SQL Server 2008, real is float(24) is a 4 byte (32 bit) floating point number.
  2. In .NET, Single is float is a 4 byte (32 bit) floating point number.
  3. In .NET, Double is an 8 byte (64 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.

2
5/24/2017 8:33:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow