CustomDateTimeOffset 1.0.1
See the version list below for details.
dotnet add package CustomDateTimeOffset --version 1.0.1
NuGet\Install-Package CustomDateTimeOffset -Version 1.0.1
<PackageReference Include="CustomDateTimeOffset" Version="1.0.1" />
paket add CustomDateTimeOffset --version 1.0.1
#r "nuget: CustomDateTimeOffset, 1.0.1"
// Install CustomDateTimeOffset as a Cake Addin #addin nuget:?package=CustomDateTimeOffset&version=1.0.1 // Install CustomDateTimeOffset as a Cake Tool #tool nuget:?package=CustomDateTimeOffset&version=1.0.1
CustomDateTime Library
CustomDateTime is a library for working with dates and time offsets in databases that do not natively support DateTimeOffset
, such as MySQL and PostgreSQL. The library allows preserving and processing time offset information using a custom CustomDateTime class, which is designed to provide functionality similar to DateTimeOffset
in databases without native support.
Features
- CustomDateTime stores information about both the time (DateTime) and the time offset (Offset).
- It provides methods for converting between DateTimeOffset and CustomDateTime.
- Includes an Entity Framework Core ValueConverter to integrate CustomDateTime directly into your models.
- Supports JSON serialization using a custom JsonConverter.
Installation
First, add the library to your project. If you're distributing the library via NuGet, you can install it as follows:
dotnet add package CustomDateTimeLibrary
If you're adding the library manually from GitHub, simply add it as a project reference.
Usage in Entity Framework Core
1. Defining Models
To use CustomDateTime in your Entity Framework Core models, simply replace DateTime
or DateTimeOffset
properties with CustomDateTime. For example:
using Core.Models.Shared;
public class Event
{
public int Id { get; set; }
// Using CustomDateTime instead of DateTimeOffset
public CustomDateTime ValidFrom { get; set; }
}
2. Configuring OnModelCreating
in DbContext
If you want to use CustomDateTime with Entity Framework Core, you'll need to add a custom ValueConverter in the OnModelCreating method in your DbContext:
using Microsoft.EntityFrameworkCore;
using Core.Converters;
using Core.Models.Shared;
public class ApplicationDbContext : DbContext
{
public DbSet<Event> Events { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.Property(e => e.ValidFrom)
.HasConversion(new CustomDateTimeConverter());
}
}
3. Registering in Program.cs
or Startup.cs
Depending on which version of ASP.NET Core you're using, you’ll need to register CustomDateTimeSerializer in Program.cs or Startup.cs to enable the correct JSON serialization and deserialization of date-time fields:
Program.cs (for .NET 6 and above):
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new CustomDateTimeSerializer());
});
var app = builder.Build();
app.UseHttpsRedirection();
app.MapControllers();
app.Run();
Startup.cs (for older versions of .NET Core):
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new CustomDateTimeSerializer());
});
}
4. Example Usage in a Controller
Once everything is set up, you can start using CustomDateTime in your ASP.NET Core API controllers. For example:
[ApiController]
[Route("api/[controller]")]
public class EventsController : ControllerBase
{
private readonly ApplicationDbContext _context;
public EventsController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet]
public async Task<IActionResult> GetEvents()
{
var events = await _context.Events.ToListAsync();
return Ok(events);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Aspire.Npgsql.EntityFrameworkCore.PostgreSQL (>= 8.2.0)
- Microsoft.EntityFrameworkCore (>= 8.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.