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

asp.net-mvc asp.net-mvc-3 code-first ef-code-first entity-framework

Question

I have now directed my MVC3 application towards an existent database that includes real data after initial development and testing.

However, I am seeing similar issue with the "datetime" column named Date1.

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'. 

Am I to understand from this that every row in the date must include a date and not be null? What should I do if I don't want to date-set a certain row?

Can someone provide suggestions on how to solve this problem?

Regards, 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; }
}
1
13
3/28/2011 10:41:22 PM

Popular Answer

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 seems that the DB you reference has no values.Date1 . Make your selection if you do not wish to specify a date for every row.Date1 attribute nullable:

public DateTime? Date1{get;set;}

As an alternative, you may simply use SQL to establish a default date for all the rows that do not presently have a datetime defined.

25
3/28/2011 10:20:07 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow