RowVersion implementation on Entity Framework for PostgreSQL
Just an updated answer for EF Core in case anyone else wanders here.
The Npgsql framework has built-in support for this using the hidden system column xmin that the OP is using in his entity as a NotMapped
property.
As referenced here, you can set the xmin column as a concurrency token within EF by calling the UseXminAsConcurrencyToken
method on your entity within its OnModelCreating
method via Fluent API (a Data Annotation is not available at this time as far as I'm aware).
For anyone already using Fluent API configurations, it's as simple as this:
public class AwesomeEntityConfiguration : IEntityTypeConfiguration<AwesomeEntity>
{
public void Configure(EntityTypeBuilder<AwesomeEntity> builder)
{
builder.UseXminAsConcurrencyToken();
}
}
/// <summary>
/// Meant to validate concurrency en database update
/// This column is updates itself in database and only works in postgresql
/// </summary>
[ConcurrencyCheck]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
//[NotMapped]
public string xmin { get; set; }
Had to add [NotMapped] attribute just for the column not to be added in the migration, commented it after database-update.