实体框架不将值插入可空的datetime字段

时间:2013-10-30 10:37:58

标签: asp.net-mvc linq entity-framework

我无法将值保存到两个可空列中。

这是我正在保存的实体:

  nquote_orderheaders header = new nquote_orderheaders()
            {
                CreatedDate = DateTime.Now,
                SendDate = DateTime.Now,
                SentByUser = accountInfo.Username,
                CreatedByUser = accountInfo.Username,
                QuoteOrderNumber = tempQuoteNumber,
                IMCustomerNumber = resellerInfo.CustomerNo,
                CustomerEmail = accountInfo.Username,
                CustomerName = resellerInfo.CustomerName,
                UserComment = "",
                StatusId = 1,
                CustomerId = data.CustomerId,
                ExpirationDate = DateTime.Now.AddDays(14)
            };

SendDate和ExpirationDate字段是可以为空的日期时间。它们在数据库中最终为null。

我正在使用MySql和MySqlConnector 6.5.4。任何建议都非常感谢。

1 个答案:

答案 0 :(得分:2)

您必须像以下代码一样将DateTime.Now强制转换为Nullable:

CreatedDate = DateTime.Now as DateTime?,

或使用更长的形式:

CreatedDate = DateTime.Now as Nullable<DateTime>,