how to check for NULL values? RRS feed

  • Question

  • User1870205858 posted

    I'm new to Entity Framework and I'm getting a null reference error that I'm not sure how to resolve.

    In my DBContext class, in the onModelCreating method, I'm binding the database columns to the class that I am using.

    My database column is called "cancelled_date" and my class property is "CancelledDate". My query that is returning the data has some records with null values in this column.

    Here's my code snippet in the onModelCreating method:


    entity.Property(e => e.CancelledDate)


    What attribute do I need to add to check for nulls? I tried putting a try/catch around this, but that did not work. 

    Any suggestions? Thanks!

    Tuesday, April 17, 2018 12:36 PM


All replies

  • User475983607 posted

    Your question is not clear.  I assume you must define the CancelledDate as a nullable type in the model.

    public class SomeClass 
        public Datetime? CancelledDate {get; set;}

    See the docs


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 17, 2018 6:30 PM
  • User1870205858 posted

    Actually, that DOES answer my question. An oversight on my part. I forgot the "?" to make the property nullable.


    Tuesday, April 17, 2018 6:58 PM