locked
Value instead of NULL [EF6 Code First] RRS feed

  • Question

  • I'm using EF6 Code First. Say, I have a table called "Consumer" with two columns:

    1) Id, int, primary key, not null

    2) Value, decimal(2,1), null

    I have the following code for Consumer class:

    Class Consumer
        Overridable Property Id As String
        Overridable Property Value As Decimal
    End Class

    And here's configuration for this class:

    Class ConsumerConfiguration : Inherits EntityTypeConfiguration(Of Consumer)
        Sub New()
            With Me
                .ToTable("Consumer")
                .Property(Function(p) p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
                .Property(Function(p) p.Value).IsOptional()
            End With
        End Sub
    End Class


    Then I write test code:

    Sub TestCodeFirst(connString As String)
    
        db = New FLContext(connString)
        db.Database.Initialize(True)
    
        Console.Write("Adding new consumer... ")
        db.Consumers.Add(New Consumer With {.Id = "032001801000175000099"})
        db.Database.ExecuteSqlCommand("insert into dbo.Consumer(Id) values ('123456789012345678901');")
    
        db.SaveChanges()
    
        Console.WriteLine("Added!")
    
    End Sub

    As you see, I insert two rows: one is through DbContext, and the other is through raw SQL.

    All works good. But to my surprise, when I execute query in SQL Server (SELECT * FROM dbo.Consumer), the value of "Value" column is "0.0" for the row inserted by DbContext, and NULL for the row inserted by raw SQL. Is it bug or I do something wrong?

    P.S. The same thing happens in EF5.

    P.P.S. If the type of Value column would be char, then inserting a row through DbContext would show NULL, as expected.


    There is no knowledge that is not power.



    • Edited by JohnyL Thursday, November 29, 2012 11:51 AM Corrected thread name
    Thursday, November 29, 2012 11:46 AM

Answers

  • Hi,

    See http://msdn.microsoft.com/en-us/library/ms235245.aspx you'll have to use the "Decimal?" datatype to handle the possibility of having a null value. Else Decimal is a value type and can't be null so when you create an object it has the default 0 value...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by JohnyL Thursday, November 29, 2012 12:15 PM
    Thursday, November 29, 2012 12:08 PM

All replies

  • Hi,

    See http://msdn.microsoft.com/en-us/library/ms235245.aspx you'll have to use the "Decimal?" datatype to handle the possibility of having a null value. Else Decimal is a value type and can't be null so when you create an object it has the default 0 value...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by JohnyL Thursday, November 29, 2012 12:15 PM
    Thursday, November 29, 2012 12:08 PM
  • Patrice, thanks a lot! It works perfectly! Thanks again!

    There is no knowledge that is not power.

    Thursday, November 29, 2012 12:15 PM