【发布时间】:2017-03-08 16:24:08
【问题描述】:
在实体框架中,我正在查询一个视图并获取与某个 id 和 datetime 匹配的记录。我想获取具有确切日期和时间作为 effDate 的记录。
var deals = context.Table
.Where(d => d.DealID == DealId && d.EffectiveDate == effDate)
.ToList();
这是数据库中的视图。
当dealId is 2013-00188 和effDate is shown above 2017/03/08 09:55:39 查询不返回任何记录,但是如果您查看数据库,您可以看到它们是匹配id 和effDate 的记录。即使我将纳秒添加到 effDate 它也不会带来任何记录。
最重要的是这个查询带回了 sql 2012 和 sql 2014 中的记录,但是 sql 2016 它没有!
我已经包含了实体框架生成的 sql,因为您可以看到日期格式不正确,并且在数据库内部,有效日期存储为 datetime 而不是 datetime2。我需要确保日期格式正确。
SELECT
[Extent1].[DealID] AS [DealID],
[Extent1].[Item] AS [Item],
[Extent1].[EffectiveDate] AS [EffectiveDate],
[Extent1].[ChargeID] AS [ChargeID],
[Extent1].[SegmentCost] AS [SegmentCost],
[Extent1].[CustomSolutionsCost] AS [CustomSolutionsCost],
[Extent1].[ItemChargeIndicator] AS [ItemChargeIndicator],
[Extent1].[LandOCost] AS [LandOCost],
[Extent1].[TreatRate] AS [TreatRate],
[Extent1].[SegmentGrossProfit] AS [SegmentGrossProfit],
[Extent1].[CustomerSpecificPrice] AS [CustomerSpecificPrice],
[Extent1].[MultipleCustSpecific] AS [MultipleCustSpecific],
[Extent1].[ListPrice] AS [ListPrice],
[Extent1].[DataSource] AS [DataSource],
[Extent1].[BOMKeyField] AS [BOMKeyField],
[Extent1].[ItemDescription] AS [ItemDescription],
[Extent1].[AdditiveType] AS [AdditiveType],
[Extent1].[AdditiveTypeDescription] AS [AdditiveTypeDescription],
[Extent1].[SpecificGravity] AS [SpecificGravity],
[Extent1].[BOMSource] AS [BOMSource],
[Extent1].[CostDate] AS [CostDate],
[Extent1].[CostSource] AS [CostSource],
[Extent1].[PriceDate] AS [PriceDate],
[Extent1].[PriceSource] AS [PriceSource],
[Extent1].[Vendor] AS [Vendor],
[Extent1].[CreatedBy] AS [CreatedBy],
[Extent1].[CreatedDate] AS [CreatedDate]
FROM [dbo].[vwDealBOM] AS [Extent1]
WHERE ([Extent1].[DealID] = @p__linq__0) AND ([Extent1].[EffectiveDate] = @p__linq__1)
-- p__linq__0: '2013-00188' (Type = AnsiString, Size = 8000)
-- p__linq__1: '08/03/2017 09:55:39' (Type = DateTime2)
任何建议都会很棒。
【问题讨论】:
-
请不要发布代码截图,尤其是开始时代码量如此微不足道。将代码文本复制/粘贴到问题中。也就是说,您应该打开 Sql Server Profiler 并查看正在执行的带有参数值的完整执行语句是什么。从那里开始,看看是否能提供任何线索。
-
我无法使用 sql profiler,因为我正在处理网络上的数据库。我已将 ef 生成的 sql 记录到控制台,它看起来很好。
-
如果您有记录的查询,您应该将其与传递的参数值一起发布在问题中。
标签: sql-server entity-framework