asp.net-mvc - EF 4.1 代码优先 : "non-null value of type ' DateTime'"Issue

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

经过初始开发和测试后,我现在将我的 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/

相关文章:

.net - Skype :{user}? 调用的 N2CMS TinyMCE filter_allowed_protocols

jquery - 为什么从 Jquery 1.5.1 更改为 Jquery 1.8.1 会破坏我在 MVC3 中的所有 CSS?

c# - 在 MVC3 中, Controller 如何知道非侵入式验证的结果?

asp.net-mvc - ASP.NET MVC 在创建用户时获取 userId?

asp.net-mvc - 可视化的DataAnnotations编辑器ASP.NET MVC?

c# - 从 dbSet 获取主键列

c# - 将 C# 函数移动到表达式以在 Entity Framework/SQL Select 中使用

javascript - ASP.NET 表单根据另一个字段中的部分显示表单字段

asp.net-mvc - 服务层与业务层 - 区别?

c# - 强制 Entity Framework 使用 SQL 参数化以更好地重用 SQL proc 缓存