【问题标题】:Entity framework not inserting values into nullable datetime field实体框架未将值插入可为空的日期时间字段
【发布时间】:2013-10-30 10:37:58
【问题描述】:

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

这是我要保存的实体:

  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 字段是可为空的日期时间。它们最终在数据库中为空。

我将 MySql 与 MySqlConnector 6.5.4 一起使用。任何建议都非常感谢。

【问题讨论】:

  • IS CreatedDate 结束,不能为空。
  • 你能粘贴到你的实体类吗?
  • 我不知道MySqlConnector,也许它对匹配类型非常挑剔。您可以尝试设置可为空的 DateTime 值:SendDate = new Nullable<DateTime>(DateTime.Now).

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


【解决方案1】:

您必须像下面的代码一样将DateTime.Now 转换为 Nullable:

CreatedDate = DateTime.Now as DateTime?,

或使用更长的形式:

CreatedDate = DateTime.Now as Nullable<DateTime>,

【讨论】:

  • 仍然保存一个空日期。
猜你喜欢
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多