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.
The latest version is available on NuGet.
dotnet add package ErikEJ.EntityFrameworkCore.SqlServer.DateOnlyTimeOnlyThe 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 |
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;