asp.net-mvc – EF 4.1 Code First:“非空值类型’DateTime’”问题

在初始开发和测试之后,我已经将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设置datetime的所有行放置认日期.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....