我有这个简单的添加到db方法,我不断收到此错误
将datetime2数据类型转换为日期时间数据类型会导致超出范围的值。声明已经终止"
在调试器中,我看到呈现的日期是" 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");
答案 0 :(得分:2)
您正在将RegesterDate添加到模型中(可能是您的参数)。这应该解决:
var entity = new UDetails()
{
// ...
Email = User.Identity.Name,
RegesterDate = DateTime.Now,
UDetailsId = User.Identity.GetUserId()
// ...
};