簡體   English   中英

DateTime屬性保持在01/01/0001

[英]DateTime property stays at 01/01/0001

我有這個簡單的添加到db方法,並且不斷收到此錯誤

datetime2數據類型到datetime數據類型的轉換導致超出范圍的值。 該語句已終止”

在調試器中,我看到渲染的日期是“ 01/01/0001 00:00:00”。 我幾乎可以確定今天不是今天,因為我通過DateTime.Now設置。

public class UDetails
{
    [Key]
    public string UDetailsId { get; set; }
    [Display(Name = "Your name")]
    public string UName { get; set; }
    public string Email { get; set; }
    [Display(Name = "Date of birth")]
    public DateTime? DOB { get; set; }
    [Display(Name = "my DOB is publich")]
    public bool IsPublic { get; set; }

    public string Gender  { get; set; }
    public DateTime? RegesterDate { get; set; }

    [Display(Name = "Occupation")]
    public string OccupationDecription { get; set; }

    public string UserPhoto { get; set; }
    public int OccupationId { get; set; }
}

var entity = new UDetails()
{
    UserPhoto = model.UserPhoto,
    DOB = model.DOB,
    IsPublic = model.IsPublic,
    Gender = model.Gender,
    OccupationDecription = model.OccupationDecription
};
model.Email = User.Identity.Name;
model.RegesterDate = DateTime.Now;
model.UDetailsId = User.Identity.GetUserId();

db.udetails.Add(entity);
db.SaveChanges();
return View("index");

您正在將RegesterDate添加到模型(可能是您的參數)。 這應該解決:

var entity = new UDetails()
{
    // ...

    Email = User.Identity.Name,
    RegesterDate = DateTime.Now,
    UDetailsId = User.Identity.GetUserId()      

    // ...
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM