1

我正在使用实体框架 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'

让我印象深刻的是 和 的映射ActivationRequestedActivationEmailSent定义相同(我刚刚updateModel在 edmx 中使用)。但ActivationRequested返回正确的值,而ActivationEmailSent返回SQLDateTime.Min( 1753-01-01 00:00:00.000)

我不知道这是否有帮助,但是当我将列设置为可为空时,它只是返回空。

4

1 回答 1

0

Found the problem for anyone else that may come with this.

The model.store properties of the DateTime column had the precision attribute set to None whereas the Entity had the precision attribute set to 3.

Changing the precision attribute to none for both fixed the issue.

Though I am still baffled why it worked for one of the columns in the first place, and not the other even though they had identical mappings.

于 2013-03-19T09:32:28.173 回答