Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 Code First: "non-null value of type 'DateTime'" Issue

After initial dev and testing I have now pointed my MVC3 application at an existing database which contains live data.

However I am getting this error related to the field called Date1 of type "datetime".

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

Is this telling me that all rows in the date must have a date and not be null? What do I do if I dont want to set a date on specific row?

Can anyone offer advice on resolving this issue?

Thanks Paul

EDIT 1 (Posting Model)

public class StaffAttachment
{
    [Key]
    public int AttachmentID { get; set; }

    public int? StaffID { get; set; }

    public int? TypeID { get; set; }

    [Required]
    [DisplayName("Proof of ID")]
    public int? AttachmentTypeID { get; set; }

    [Required]
    [DisplayName("ID Reference")]
    public string Reference1 { get; set; }

    public string Reference2 { get; set; }

    public string DateType { get; set; }

    [DisplayName("Expiry Date")]
    public DateTime Date1 { get; set; }

    [Required]
    public DateTime Date2 { get; set; }

    public DateTime Date3 { get; set; }

    public DateTime Date4 { get; set; }

    public string AttachmentPath { get; set; }

    public int? ValidatedUserID { get; set; }

    public DateTime ValidatedDate { get; set; }

    public int? CreatedUserID { get; set; }

    public DateTime CreatedDate { get; set; }

    public int? EditedUserID { get; set; }

    public DateTime EditedDate { get; set; }

    public TimeSpan TimeStamp { get; set; }

    public string FileName { get; set; }

    public byte[] FileImage { get; set; }

    public int AttachmentSection { get; set; }
}
like image 855
Paul Brown Avatar asked Mar 28 '11 22:03

Paul Brown


People also ask

What is the null value of DateTime?

DateTime is a Value Type like int, double etc. so there is no way to assigned a null value.

Does DateTime accept null?

DateTime CAN be compared to null; It cannot hold null value, thus the comparison will always be false. DateTime is a "Value Type". Basically a "value type" can't set to NULL. But by making them to "Nullable" type, We can set to null.

Should DateTime be nullable?

DateTime itself is a value type. It cannot be null. Show activity on this post. No -- DateTime is a struct in C# and structs (value types) can not be null.


2 Answers

Is this telling me that all rows in the date must have a date and not be null? What do I do if I dont want to set a date on specific row?

It looks like the DB you point at have null values for Date1. If you do indeed not want to set a date for all rows, just make your Date1 property nullable:

public DateTime? Date1{get;set;}

Alternatively you can just put in a default date for all the rows that do not have a datetime set currently via SQL.

like image 143
BrokenGlass Avatar answered Sep 18 '22 05:09

BrokenGlass


public DateTime? Date1 { get; set; }

use this, your model needs to be told that there can be null values inside your database, otherwise it will assume there isn't.

like image 25
Lewis Avatar answered Sep 22 '22 05:09

Lewis