2013-03-17 34 views
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] PRIMARY KEY CLUSTERED

這是我使用來檢索特定的行的代碼:

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.Min1753-01-01 00:00:00.000

我不知道這是否會有所幫助,但是當我設置列是空的,它只是返回null。

+0

在代碼中的某處,該值沒有設置。 – scartag 2013-03-17 22:15:31

回答

0

發現問題的其他人可能會與此相伴。

日期時間列的model.store性質有精度屬性設置爲而實體具有精度屬性設置爲。

將精度屬性更改爲對於這兩個問題都是固定的。

儘管我仍然困惑,爲什麼它首先爲其中一列工作,而不是其他人,儘管他們有相同的映射。

+0

沒有爲我工作 – Dileep 2017-11-20 16:24:32

相關問題