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

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 displays in Entity Framework as follows:

[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 method below 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 doesn't work for validAcctProbs

when he puts it in a list, with an 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 doubled in EF.

EDIT: I don't think this is cheating because the previous question was specifically about SQLite and the erroneous mappings in this driver. This is for Microsoft TSQL.

+3


source to share


1 answer


I believe reals in TSQL are doubled in EF

The documentation is a bit ambiguous. We need to find it in the implementation. the source code for EF6 is publicly available, 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 you why this makes sense:



  • In T-SQL, starting in SQL Server 2008, real - float (24) is a 4 byte (32 bit) floating point number.
  • In .NET, Single float

    is a 4-byte (32-bit) floating point number.
  • In .NET, Double is an 8-byte (64-bit) floating point number.

Range real

: -3.40e38 to 3.40e38
Range Single

: -3.402823e38 to + 3.402823e38

So it doesn't make sense to use a type double?

for your field Probability

, because real

precision never runs out Single

.

+1


source







All Articles