I am using Entity Framework 6 with PostgreSQL. I have an entity in which I want to prevent concurrency issues, following this documentation I added a RowVersion property with [Timestamp] attribute, however after saving changes to the entity the column RowVersion value stays the same in the database.
[Timestamp]
public byte[] RowVersion { get; set; }
Am I missing something or is there another way to handle it in PostgreSQL?
Furthermore, Entity Framework's design makes it particularly friendly for PostgreSQL developers. Entity Framework (EFCore) Core is a lighter weight and more flexible version that specifically enables . NET objects. It reduces the amount of data access code developers need to write, and offers higher-performance APIs.
If you do want to implement this approach to concurrency, you have to mark all non-primary-key properties in the entity you want to track concurrency for by adding the ConcurrencyCheck attribute to them. That change enables the Entity Framework to include all columns in the SQL WHERE clause of UPDATE statements.
PostgreSQL is the open source EF Core provider for PostgreSQL. It allows you to interact with PostgreSQL via the most widely-used . NET O/RM from Microsoft, and use familiar LINQ syntax to express queries. It's built on top of Npgsql.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With