EF 4.1 Code First:“‘DateTime’类型的非空值”问题

发布于 2024-10-27 03:37:43 字数 1557 浏览 1 评论 0原文

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

这是否告诉我日期中的所有行都必须有日期且不能为空?如果我不想在特定行上设置日期该怎么办?

任何人都可以提供解决此问题的建议吗?

谢谢 Paul

编辑 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; }
}

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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琴流音 2024-11-03 03:37:43

这是否告诉我所有行都在
日期必须有日期,而不是
无效的?如果我不想怎么办
在特定行设置日期?

看起来您指向的数据库 Date1 具有空值。如果您确实不想为所有行设置日期,只需将您的 Date1 属性设为可为空即可:

public DateTime? Date1{get;set;}

或者,您也可以为当前未设置日期时间的所有行输入默认日期SQL。

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.

背叛残局 2024-11-03 03:37:43
public DateTime? Date1 { get; set; }

使用这个,你的模型需要被告知数据库中可以有空值,否则它会假设不存在。

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.

秋日私语 2024-11-03 03:37:43

使用这个:

public Nullable<DateTime> LoginDate { get; set; }

Use this :

public Nullable<DateTime> LoginDate { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文