经过初始开发和测试后,我现在将我的 MVC3 应用程序指向包含实时数据的现有数据库。
但是,我收到与“datetime”类型的名为 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'.
这是否告诉我日期中的所有行都必须有日期且不能为空?如果我不想在特定行上设置日期该怎么办?
任何人都可以提供解决此问题的建议吗?
谢谢保罗
编辑1(发布模型)
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; }
}
最佳答案
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?
您指向的数据库似乎 Date1
具有空值。如果您确实不想为所有行设置日期,只需将您的 Date1
属性设为可为空即可:
public DateTime? Date1{get;set;}
或者,您可以为当前未通过 SQL 设置日期时间的所有行输入默认日期。
关于asp.net-mvc - EF 4.1 代码优先 : "non-null value of type ' DateTime'"Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5465686/