EFCore.SqlServer.DateOnlyTimeOnly

Use the .NET types DateOnly and TimeOnly with the EF Core 6 and 7 SQL Server provider

MIT License

Stars
42
Committers
2

ErikEJ.EntityFrameworkCore.SqlServer.DateOnlyTimeOnly

Adds .NET 6 or later DateOnly and TimeOnly support to the SQL Server EF Core provider. These types map directly to the SQL Server date and time data types.

Installation

The latest version is available on NuGet.

dotnet add package ErikEJ.EntityFrameworkCore.SqlServer.DateOnlyTimeOnly

Compatibility

The following table show which version of this library to use with which version of EF Core.

EF Core Version to use
6.0 6.0.x
8.0+ Built-in

Usage

Enable DateOnly and TimeOnly support by calling UseDateOnlyTimeOnly inside UseSqlServer. UseSqlServer is is typically called inside Startup.ConfigureServices or OnConfiguring of your DbContext type.

options.UseSqlServer(
    connectionString,
    x => x.UseDateOnlyTimeOnly());

Add DateOnly and TimeOnly properties to your entity types. Or reverse engineer a table with date and time columns.

class EventSchedule
{
    public int Id { get; set; }
    public DateOnly StartDate { get; set; }
    public TimeOnly TimeOfDay { get; set; }
}

Insert data.

dbContext.Add(new EventSchedule { StartDate = new DateOnly(2022, 12, 24), TimeOfDay = new TimeOnly(12, 00) });
dbContext.SaveChanges();

Query.

var eventsOfTheDay = from e in dbContext.EventSchedules
                     where e.StartDate == new DateOnly(2022, 10, 12)
                     select e;

See also

Badges
Extracted from project README
latest version downloads