簡體   English   中英

將 DBFunction 從 Entity Framework 6 遷移到 Entity Framework Core

[英]Migrating DBFunctions from Entity Framework 6 to Entity framework Core

我正在將我的 .Net 6 項目從 Entity Framework 6 遷移到 Entity Framework Core,我需要替換以下代碼。 如何將此 Entity Framework 6 Sql TruncateTime 函數替換為與 EF Core 一起使用的函數。

var result = db.Tickets
               .Where(x => DbFunctions.TruncateTime(x.DateTimeCreated) == statCalcDefinitionDateTime.Date)

正如 Svyatoslav Danyliv 在評論中所說。 實體框架核心現在支持“DateTime.Date”,因此不再需要“dbFunctions.TruncateTime()”函數。

Svyatoslav Danyliv 還建議我在 where 語句中使用范圍而不是日期,我同意。 Blow 是“DBFunctions.TruncateTime()”的新實體框架核心方式

var result = db.Tickets
               .Where(x => x.DateTimeCreated.Date == statCalcDefinitionDateTime.Date)

或者,您也可以使用日期范圍

var result = db.Tickets
               .Where(x => 
                      x.DateTimeCreated >= statCalcDefinitionDateTime.Date && 
                      x.DateTimeCreated < statCalcDefinitionDateTime.Date.AddDays(1))

注意:我也在這里找到了相同的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM