The conversion of a datetime2 data type to a datetime data type Error

后端 未结 9 1592
慢半拍i
慢半拍i 2020-12-30 08:45

I have a controller:

[HttpPost]
public ActionResult Create(Auction auction)
{
    var db = new EbuyDataContext();
    db.Auctions.Add(auction);
    db.SaveCh         


        
9条回答
  •  臣服心动
    2020-12-30 09:09

    I had the same problem when I tried to save an unassigened DateTime member to the DB, while using EntityFramework. I read the answers here, and I learned that it can be solved by declaring the member as nullable. I tried it, and it worked! So here is a code snap, for those who need it:

    public Nullable MyDateTime { get; set; }
    

    or

    public DateTime? MyDateTime { get; set; }
    

    Later I could use it like bellow:

    if(MyDateTime.HasValue)
    {
        DoBlaBla(MyDateTime.Value);
    }
    

    or just assign a value to it like nothing had happen...

    MyDateTime = DateTime.Now;
    

提交回复
热议问题