我正在使用实体框架 5,数据库优先。
我正在做我以前做过很多次的事情,但由于某种原因它不起作用。
我的表定义为:
CREATE TABLE [dbo].[ActivationQueue](
[ID] [int] IDENTITY(1,1) NOT NULL,
[username] [nvarchar](255) NOT NULL,
[email] [nvarchar](255) NOT NULL,
[ActivationRequested] [datetime] NOT NULL,
[ActivationEmailSent] [datetime] NOT NULL,
[AccountActivated] [datetime] NOT NULL,
[ActivationAttempts] [int] NULL,
[ActivationKey] [uniqueidentifier] NULL,
约束 [PK_Activations] 主键集群
这是我用来检索特定行的代码:
ActivationQueue accountActivation = DbSet.FirstOrDefault(a => a.username.ToLower() == userName)
当我DbSet.FirstOrDefault()
用来获取第一行时,ActivationEmailSent 列总是返回SQLDateTime.MinValue()
,即使它具有正确的值。ActivationRequested 列始终返回正确的值。
我使用了 SQL 探查器,当我运行下面的 SQL 时,我得到了正确的DateTime
值。
exec sp_executesql N'SELECT TOP (1)
[Extent1].[ID] AS [ID],
[Extent1].[username] AS [username],
[Extent1].[email] AS [email],
[Extent1].[ActivationRequested] AS [ActivationRequested],
[Extent1].[ActivationEmailSent] AS [ActivationEmailSent],
[Extent1].[AccountActivated] AS [AccountActivated],
[Extent1].[ActivationAttempts] AS [ActivationAttempts],
[Extent1].[ActivationKey] AS [ActivationKey]
FROM [dbo].[ActivationQueue] AS [Extent1]
WHERE (LOWER([Extent1].[username])) = @p__linq__0',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'someusername'
让我印象深刻的是 和 的映射ActivationRequested
与ActivationEmailSent
定义相同(我刚刚updateModel
在 edmx 中使用)。但ActivationRequested
返回正确的值,而ActivationEmailSent
返回SQLDateTime.Min
( 1753-01-01 00:00:00.000
)
我不知道这是否有帮助,但是当我将列设置为可为空时,它只是返回空。