Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RowVersion implementation on Entity Framework for PostgreSQL

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?

like image 322
Silvano González Avatar asked Mar 07 '17 14:03

Silvano González


People also ask

Can you use Entity Framework with 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.

How do you configure entity framework for optimistic concurrency?

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.

Does EF core support PostgreSQL?

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.


2 Answers

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();
    }
}
like image 150
Dan Walsh Avatar answered Oct 24 '22 05:10

Dan Walsh


/// <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.

like image 43
Silvano González Avatar answered Oct 24 '22 05:10

Silvano González