Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DateOnly.FromDateTime translation #11

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<RootNamespace>Microsoft.EntityFrameworkCore.SqlServer</RootNamespace>
<Description>Adds DateOnly and TimeOnly support to the SQL Server EF Core 7 provider</Description>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageReleaseNotes>Update to Microsoft.Data.SqlClient 5.1.1</PackageReleaseNotes>
<Version>7.0.2</Version>
<PackageReleaseNotes>Add DateOnly.FromDateTime translation</PackageReleaseNotes>
<Version>7.0.3</Version>
<PackageId>ErikEJ.EntityFrameworkCore.SqlServer.DateOnlyTimeOnly</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
17 changes: 14 additions & 3 deletions EFCore.SqlServer.DateOnlyTimeOnly.Test/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Microsoft.EntityFrameworkCore.SqlServer
{
public class QueryTests : IDisposable
{
private readonly DateAndTimeContext _db;
private readonly DateAndTimeContextQuery _db;

private string SelectStatement => "SELECT [e].[Id], [e].[StartDate], [e].[StartTime] FROM [Events] AS [e]";
private string SelectStatement => "SELECT [e].[Id], [e].[LegacyDateTime], [e].[StartDate], [e].[StartTime] FROM [Events] AS [e]";

public QueryTests()
{
_db = new DateAndTimeContext();
_db = new DateAndTimeContextQuery();
_db.Database.EnsureDeleted();
_db.Database.EnsureCreated();
}
Expand Down Expand Up @@ -211,6 +211,17 @@ public async Task DateOnly_DateDiff_Day()
Assert.Single(results);
}

[Fact]
public async Task DateOnly_FromDateTime()
{
var results = await _db.Events.Where(e => DateOnly.FromDateTime(e.LegacyDateTime) >= new DateOnly(2000, 1, 1)).ToListAsync();
Assert.Equal(
condense(@$"{SelectStatement} WHERE CONVERT(date, [e].[LegacyDateTime]) >= '2000-01-01'"),
condense(_db.Sql));

Assert.Single(results);
}

[Fact]
public async Task TimeOnly_AddHours()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore.SqlServer.Test.Logging;
using System;

namespace Microsoft.EntityFrameworkCore.SqlServer.Test.Models
{
class DateAndTimeContextQuery : DbContext
{
readonly TestLoggerFactory _loggerFactory
= new TestLoggerFactory();

public DbSet<EventScheduleQuery> Events { get; set; }

public string Sql
=> _loggerFactory.Logger.Sql;

protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options
.UseSqlServer(
@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DateOnlyTimeOnlyTests",
x => x.UseDateOnlyTimeOnly())
.UseLoggerFactory(_loggerFactory);

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EventScheduleQuery>()
.HasData(
new EventScheduleQuery { Id = 1, StartDate = new DateOnly(2022, 12, 13), StartTime = new TimeOnly(9, 9, 9, 9) },
new EventScheduleQuery { Id = 2, StartDate = new DateOnly(2022, 12, 24), StartTime = new TimeOnly(10, 10, 10), LegacyDateTime = new DateTime(2022, 11, 24) },
new EventScheduleQuery { Id = 3, StartDate = new DateOnly(1758, 1, 1), StartTime = new TimeOnly(11, 11) });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Microsoft.EntityFrameworkCore.SqlServer.Test.Models
{
class EventScheduleQuery : EventSchedule
{
public DateTime LegacyDateTime { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<RootNamespace>Microsoft.EntityFrameworkCore.SqlServer</RootNamespace>
<Description>Adds DateOnly and TimeOnly support to the SQL Server EF Core 6 provider</Description>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageReleaseNotes>Update to Microsoft.Data.SqlClient 5.1.1</PackageReleaseNotes>
<Version>6.0.2</Version>
<PackageReleaseNotes>Add DateOnly.FromDateTime translation</PackageReleaseNotes>
<Version>6.0.3</Version>
<PackageId>ErikEJ.EntityFrameworkCore.SqlServer.DateOnlyTimeOnly</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public SqlServerDateOnlyMethodTranslator(
instance.TypeMapping);
}

if (method.DeclaringType == typeof(DateOnly)
&& method.Name == nameof(DateOnly.FromDateTime)
&& arguments.Count == 1)
{
return _sqlExpressionFactory.Function(
ErikEJ marked this conversation as resolved.
Show resolved Hide resolved
"CONVERT",
new[] { _sqlExpressionFactory.Fragment("date"), arguments[0] },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
typeof(DateOnly),
_typeMappingSource.FindMapping(typeof(DateOnly), "date"));
}

return null;
}
}