Skip to content

Commit 1a8b5a1

Browse files
committed
Additional translations for SQL Server DateTimeOffset
Closes #26781 Closes #38074 Closes #34756
1 parent 6abde55 commit 1a8b5a1

File tree

15 files changed

+891
-28
lines changed

15 files changed

+891
-28
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,7 @@ ASALocalRun/
342342
# Local History for Visual Studio
343343
.localhistory/
344344
full.targets.txt
345+
346+
347+
# Language Server cache
348+
*.lscache

src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ public virtual Expression Translate(SelectExpression selectExpression, Expressio
9898
{
9999
switch (expression)
100100
{
101-
case NewExpression or MemberInitExpression or StructuralTypeShaperExpression or IncludeExpression:
101+
// In index-based binding mode, NewExpression falls through to the TranslateProjection path below,
102+
// which allows provider-specific VisitNew overrides to translate constructors to SQL functions
103+
// (e.g. new DateTimeOffset(DateTime, TimeSpan) → TODATETIMEOFFSET). If translation fails,
104+
// the fallback base.Visit(expression) call routes to VisitNew for client-side DTO construction.
105+
case NewExpression when !_indexBasedBinding:
106+
case MemberInitExpression or StructuralTypeShaperExpression or IncludeExpression:
102107
return base.Visit(expression);
103108

104109
case null:

src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,202 @@ public static DateTimeOffset AtTimeZone(
21672167
string timeZone)
21682168
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(AtTimeZone)));
21692169

2170+
#region DateTrunc
2171+
2172+
/// <summary>
2173+
/// Truncates the <paramref name="date" /> to the specified <paramref name="datepart" /> precision.
2174+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2175+
/// </summary>
2176+
/// <remarks>
2177+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2178+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2179+
/// for more information and examples.
2180+
/// </remarks>
2181+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2182+
/// <param name="datepart">
2183+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2184+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2185+
/// <c>microsecond</c>.
2186+
/// </param>
2187+
/// <param name="date">The value to truncate.</param>
2188+
/// <returns>The truncated <see cref="DateTime" /> value.</returns>
2189+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2190+
public static DateTime DateTrunc(
2191+
this DbFunctions _,
2192+
[NotParameterized] string datepart,
2193+
DateTime date)
2194+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2195+
2196+
/// <summary>
2197+
/// Truncates the <paramref name="date" /> to the specified <paramref name="datepart" /> precision.
2198+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2199+
/// </summary>
2200+
/// <remarks>
2201+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2202+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2203+
/// for more information and examples.
2204+
/// </remarks>
2205+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2206+
/// <param name="datepart">
2207+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2208+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2209+
/// <c>microsecond</c>.
2210+
/// </param>
2211+
/// <param name="date">The value to truncate.</param>
2212+
/// <returns>The truncated <see cref="DateTime" /> value.</returns>
2213+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2214+
public static DateTime? DateTrunc(
2215+
this DbFunctions _,
2216+
[NotParameterized] string datepart,
2217+
DateTime? date)
2218+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2219+
2220+
/// <summary>
2221+
/// Truncates the <paramref name="dateTimeOffset" /> to the specified <paramref name="datepart" /> precision.
2222+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2223+
/// </summary>
2224+
/// <remarks>
2225+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2226+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2227+
/// for more information and examples.
2228+
/// </remarks>
2229+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2230+
/// <param name="datepart">
2231+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2232+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2233+
/// <c>microsecond</c>.
2234+
/// </param>
2235+
/// <param name="dateTimeOffset">The value to truncate.</param>
2236+
/// <returns>The truncated <see cref="DateTimeOffset" /> value.</returns>
2237+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2238+
public static DateTimeOffset DateTrunc(
2239+
this DbFunctions _,
2240+
[NotParameterized] string datepart,
2241+
DateTimeOffset dateTimeOffset)
2242+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2243+
2244+
/// <summary>
2245+
/// Truncates the <paramref name="dateTimeOffset" /> to the specified <paramref name="datepart" /> precision.
2246+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2247+
/// </summary>
2248+
/// <remarks>
2249+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2250+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2251+
/// for more information and examples.
2252+
/// </remarks>
2253+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2254+
/// <param name="datepart">
2255+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2256+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2257+
/// <c>microsecond</c>.
2258+
/// </param>
2259+
/// <param name="dateTimeOffset">The value to truncate.</param>
2260+
/// <returns>The truncated <see cref="DateTimeOffset" /> value.</returns>
2261+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2262+
public static DateTimeOffset? DateTrunc(
2263+
this DbFunctions _,
2264+
[NotParameterized] string datepart,
2265+
DateTimeOffset? dateTimeOffset)
2266+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2267+
2268+
/// <summary>
2269+
/// Truncates the <paramref name="date" /> to the specified <paramref name="datepart" /> precision.
2270+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2271+
/// </summary>
2272+
/// <remarks>
2273+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2274+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2275+
/// for more information and examples.
2276+
/// </remarks>
2277+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2278+
/// <param name="datepart">
2279+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2280+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2281+
/// <c>microsecond</c>.
2282+
/// </param>
2283+
/// <param name="date">The value to truncate.</param>
2284+
/// <returns>The truncated <see cref="DateOnly" /> value.</returns>
2285+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2286+
public static DateOnly DateTrunc(
2287+
this DbFunctions _,
2288+
[NotParameterized] string datepart,
2289+
DateOnly date)
2290+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2291+
2292+
/// <summary>
2293+
/// Truncates the <paramref name="date" /> to the specified <paramref name="datepart" /> precision.
2294+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2295+
/// </summary>
2296+
/// <remarks>
2297+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2298+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2299+
/// for more information and examples.
2300+
/// </remarks>
2301+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2302+
/// <param name="datepart">
2303+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2304+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2305+
/// <c>microsecond</c>.
2306+
/// </param>
2307+
/// <param name="date">The value to truncate.</param>
2308+
/// <returns>The truncated <see cref="DateOnly" /> value.</returns>
2309+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2310+
public static DateOnly? DateTrunc(
2311+
this DbFunctions _,
2312+
[NotParameterized] string datepart,
2313+
DateOnly? date)
2314+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2315+
2316+
/// <summary>
2317+
/// Truncates the <paramref name="time" /> to the specified <paramref name="datepart" /> precision.
2318+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2319+
/// </summary>
2320+
/// <remarks>
2321+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2322+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2323+
/// for more information and examples.
2324+
/// </remarks>
2325+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2326+
/// <param name="datepart">
2327+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2328+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2329+
/// <c>microsecond</c>.
2330+
/// </param>
2331+
/// <param name="time">The value to truncate.</param>
2332+
/// <returns>The truncated <see cref="TimeOnly" /> value.</returns>
2333+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2334+
public static TimeOnly DateTrunc(
2335+
this DbFunctions _,
2336+
[NotParameterized] string datepart,
2337+
TimeOnly time)
2338+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2339+
2340+
/// <summary>
2341+
/// Truncates the <paramref name="time" /> to the specified <paramref name="datepart" /> precision.
2342+
/// Corresponds to SQL Server's <c>DATETRUNC(datepart, date)</c>.
2343+
/// </summary>
2344+
/// <remarks>
2345+
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
2346+
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
2347+
/// for more information and examples.
2348+
/// </remarks>
2349+
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
2350+
/// <param name="datepart">
2351+
/// The datepart to truncate to. Can be one of: <c>year</c>, <c>quarter</c>, <c>month</c>, <c>dayofyear</c>,
2352+
/// <c>day</c>, <c>week</c>, <c>iso_week</c>, <c>hour</c>, <c>minute</c>, <c>second</c>, <c>millisecond</c>,
2353+
/// <c>microsecond</c>.
2354+
/// </param>
2355+
/// <param name="time">The value to truncate.</param>
2356+
/// <returns>The truncated <see cref="TimeOnly" /> value.</returns>
2357+
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/datetrunc-transact-sql">SQL Server documentation for <c>DATETRUNC</c>.</seealso>
2358+
public static TimeOnly? DateTrunc(
2359+
this DbFunctions _,
2360+
[NotParameterized] string datepart,
2361+
TimeOnly? time)
2362+
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateTrunc)));
2363+
2364+
#endregion DateTrunc
2365+
21702366
/// <summary>
21712367
/// Returns the starting position of the first occurrence of a pattern in a specified expression, or zero if the pattern is not found, on
21722368
/// all valid text and character data types.

src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)