Search code examples
c#postgresqlentity-framework-corenpgsql

Set DateStyle to DMY On Database Migration


By default if the database is not created the migration will handle creation. However it will set the DateStyle to ISO, MDY. I want it to be DMY, yet I haven't found a default way to do it in OnModelCreating or OnConfiguring using Npgsql and Entity-Framework Core.

Is this possible to do in Npgsql migrations?


Solution

  • DateStyle isn't a database property (i.e. managed when creating the database), it's a connection parameter that simply manages how date/time values are displayed, i.e. converted to strings. This shouldn't affect the workings of Entity Framework Core itself in any way - EF (or rather Npgsql) reads and writes dates in binary encoding.

    If for some reason you need to fetch dates as strings, you can always set DateStyle yourself as suggested by @laurenz-albe. This can be done in EFCore by simply doing ctx.Database.ExecuteSqlCommand("SET DateStyle = 'ISO, DMY'") where ctx is your DbContext.