locked
The xxx property on 'yyy' could not be set to a 'null' value. You must set this property to a non-null value of type 'DateTime'. Error RRS feed

  • Question

  • Although my database-column allows null values I'm getting an error message:

    The 'CreatedDateTime' property on 'Country' could not be set to a 'null' value. You must set this property to a non-null value of type 'DateTime'.

    What goes wrong here and how an i solve this in my code?

    Thanks.

    My database table:

    CREATE TABLE [dbo].[Country](
     [Id] [uniqueidentifier] NOT NULL,
     [Code] [char](2) NOT NULL,
     [CreatedDateTime] [datetime] NULL 
     CONSTRAINT [PK_Country_1] PRIMARY KEY CLUSTERED

    My Country class:

    private Guid id;
    private string code;
    private DateTime createdDateTime;
    ...

    public Guid Id
    get
    {
    return this.id;
    }
    set
    {
    this.id = value;
    }

    public string Code
    {
       get
       {
          return this.code;
       }
       set
       {
          this.code = value;
       }
    }

    public DateTime CreatedDateTime
    {           
      get
      {
         return this.createdDateTime;
      }
      set
      {
         this.createdDateTime = value;
      }
    }
    ...

    The 'CreatedDateTime' property on 'Country' could not be set to a 'null' value. You must set this property to a non-null value of type 'DateTime'.

    Monday, April 4, 2011 5:41 PM

Answers

  • Thanks but I'm using Code First (not edmx).

    I suppose I should modify my code as follows?

    private DateTime? createdDateTime;
    ...

    public DateTime? CreatedDateTime
    {
      get { return this.createdDateTime; }
      set { this.createdDateTime = value; }
    }
    ...

    • Proposed as answer by Alan_chen Tuesday, April 5, 2011 2:11 AM
    • Marked as answer by Alan_chen Wednesday, April 20, 2011 1:59 AM
    Monday, April 4, 2011 5:59 PM

All replies

  • Open the EDMX designer, find your Country entity, select the CreatedDateTime property, and set "Nullable" to True.  Obviously from your code, CreatedDateTime is a DateTime - not a Nullable<DateTime> as it would need to be.
    Monday, April 4, 2011 5:44 PM
  • Thanks but I'm using Code First (not edmx).

    I suppose I should modify my code as follows?

    private DateTime? createdDateTime;
    ...

    public DateTime? CreatedDateTime
    {
      get { return this.createdDateTime; }
      set { this.createdDateTime = value; }
    }
    ...

    • Proposed as answer by Alan_chen Tuesday, April 5, 2011 2:11 AM
    • Marked as answer by Alan_chen Wednesday, April 20, 2011 1:59 AM
    Monday, April 4, 2011 5:59 PM
  • Yes that is how you will want to code it. Make sure that if you have a nullable column in the database that you specify a nullable in the POCO object.
    Devlin Liles http://twitter.com/devlinliles http://www.devlinliles.com/ If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, April 4, 2011 9:05 PM
  • ok thanks.

    last question what's confusing to me: when mapping with code first (EF4.1 RC) you have the options: IsOptional() and IsRequired() when should they be used?

    eg
    ...
    this.Property(c => c.CreatedDateTime)
       .HasColumnName("CreatedDateTime")
       .IsOptional();

    Thanks.

    Monday, April 4, 2011 9:23 PM
  • On properties of foreign keys you should mark these either optional or required based on referencing.
    Devlin Liles http://twitter.com/devlinliles http://www.devlinliles.com/ If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, April 4, 2011 9:26 PM
  • Hi GuyD,

    Welcome!

    IsOptional() and IsRequired() are used to configure database column, IsOptional()----database column is null/IsRequired() column is not null.

    I think the link here is help: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 5, 2011 2:17 AM