Open Closed

CreationTime from AuditedAggregateRoot is persisted as timezone without timezone in Postgres #1067


User avatar
0
Leonardo.Willrich created
  • ABP Framework version: v4.2.2
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes / no
  • Exception message and stack trace:
  • Steps to reproduce the issue:

Hi,

I am creating most of my entities descending from the clas AuditedAggregateRoot. It implements the field CreationTime and LastModificationTime. Using Postgres database and Entity Framework Code First, it is creating those fields as Timestamp without timezone. In my opinion, it should be with timezone instead.

I realized that if a class is defined as DateTime, Npgsql will create that field as "timestamp without timezone". However, if the field is defined as DateTimeOffset, then it creates as "timestamp with timezone".

I think I have basically two options to deal with that:

  1. Adding a definition in ModelCreatingExtensions class on Configure method. So, I can use the method HasColumnType for the Property. For example:
builder.Entity<Cause>(b => {
                b.ToTable("Causes");
                b.ConfigureByConvention();
                b.Property(x => x.CreationTime).HasColumnType("timestamp with time zone");
                b.Property(x => x.LastModificationTime).HasColumnType("timestamp with time zone");
            });
  1. Manipulating the class Migration generated by "Add-Migration", then I can just change the type property. See below:
public partial class Create_Cause : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Causes",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    Name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    DefaultCause = table.Column<bool>(type: "boolean", nullable: false),
                    MarkerColor = table.Column<int>(type: "integer", nullable: false),
                    MakerType = table.Column<int>(type: "integer", nullable: false),
                    ExtraProperties = table.Column<string>(type: "text", nullable: true),
                    ConcurrencyStamp = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: true),
                    
                    // Before
                    // CreationTime = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
                    // After
                    CreationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    
                    CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
                    
                    // LastModificationTime = table.Column<DateTime>(type: "timestamp without time zone", nullable: true),
                    LastModificationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    
                    LastModifierId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Causes", x => x.Id);
                });
        }

I am not sure if there is other way to do that. Do you think will be a solution in the framework for this issue in future? What is recommended to do at this situation?


2 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    Can you try to set your DateTime Kind to UTC This way, it'll support UTC

    Configure<AbpClockOptions>(options => options.Kind = DateTimeKind.Utc);
    

    https://github.com/abpframework/abp/issues/4067

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11