In my Blazor Server app I'm using EF Core with Npgsql to database-first scaffold my DbContext and model classes. When I set it up, I followed the advice at Npgsql and went with NodaTime. Which means I installed these NuGet packages:
- Npgsql.EntityFrameworkCore.PostgreSQL
- Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime
- Npgsql.NodaTime
I would run this command for scaffolding:
dotnet ef dbcontext scaffold [connection-string-redacted] Npgsql.EntityFrameworkCore.PostgreSQL ...
Now (weeks later) I've decided that NodaTime is overkill for my specific situation, even though I fully appreciate why it exists and the multitude of problems it solves. And I'd like to scaffold to .NET native types instead (aka System.DateTime).
So my first attempt was to comment out the line in Program.cs where I configure Npgsql to use NodaTime:
builder.Services
.AddDbContextFactory<TheDbContext>(options =>
{
options.UseNpgsql(dbConnection, conf =>
{
//conf.UseNodaTime();
conf.EnableRetryOnFailure();
conf.CommandTimeout(2);
});
});
Running scaffolding after this change still generated NodaTime classes (NodaTime.LocalDateTime) for Postgres columns of type timestamp.
What do I need to do to get the cli scaffolding to stop using NodaTime?
Turns out the cli scaffolding does not care what is configured in Program.cs. It relies on the NuGet packages installed in the project.
And as long as Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime was still installed, scaffolding would use NodaTime.
Even though I did not explicitly specify it in the cli command (it just had Npgsql.EntityFrameworkCore.PostgreSQL).
The fix was to uninstall this NuGet Package from the project: Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime
Then scaffolding generated the desired .NET Native types (aka System.DateTime).