I'm learning aspnet.core
In laravel we can do $table->timestamps()
to generate created_at
and modified_at
to be updated automatically.
After searching, I found out that we can do this in EFCore (i'm using postgresql with npgsql
modelBuilder.Entity<User>()
.Property(u => u.created_at)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.ValueGeneratedOnAdd();
modelBuilder.Entity<User>()
.Property(u => u.modified_at)
.HasComputedColumnSql("CURRENT_TIMESTAMP")
.ValueGeneratedOnUpdate();
However I'm getting error:
Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] ALTER TABLE "Users" ADD modified_at timestamp without time zone GENERATED ALWAYS AS (CURRENT_TIMESTAMP) STORED; Npgsql.PostgresException (0x80004005): 42P17: generation expression is not immutable at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
I suspect the HasComputedColumnSql
has couple of problem but I can't figure out why. Can anyone help?
Thanks
User contributions licensed under CC BY-SA 3.0