Optimistic concurrency: IsConcurrencyToken and RowVersion

c# concurrency ef-code-first entity-framework sql-server

Question

I'm establishing the standard concurrency strategy I'll employ in my application.

I choose to adopt a positive approach.

My entire database is mapped asTable per Type (TPT) (Using ancestry). I immediately discovered that utilizing columns of type RowVersion with inheritance on Entity Framework has a problem:

Product

Id INT IDENTITY PRIMARY KEY
RowVersion ROWVERSION

Car (inherits Product records)

Color TYNIINT NOT NULL,
AnotherProperty....   

Whenever I alter a record of theCar the RowVersion column from tableProduct table won't be changed.

I'm going to use a type column.datetime2 (7) in Product and manually update it if any records in the tables that inherit from it are changed.

I feel like I'm remaking the wheel.

Is the optimistic concurrency strategy also applicable toROWVERSION the use ofTable per Type (TPT) Entity Framework, please?

Edit

I mapped:

class Product
{
    int Id { get; set; }
    string Name { get; set; }
    byte[] RowVersion { get; set; }
}

class Car : Product
{
    int Color { get; set; }
}

conventions for CodeFirst.

The RowVersion property is the onlyProduct entity's definitions are unique:

modelBuilder.Entity<Product>() 
    .Property(t => t.RowVersion) 
    .IsConcurrencyToken();
1
13
8/9/2016 5:36:41 PM

Accepted Answer

When dealing with SQL Server, you must use this mapping in both EF6 and EF-core:

modelBuilder.Entity<Product>() 
.Property(t => t.RowVersion) 
.IsRowVersion(); // Not: IsConcurrencyToken

A property is configured as a concurrency token by IsConcurrencyToken, however (when used for abyte[] property)

  • the type of data isvarbinary(max)
  • its worth remains constantnull In the absence of initialization
  • If a record is modified, its value is not automatically incremented.

On the other side, IsRowVersion

  • possess datatyperowversion either in SQL Servertimestamp in previous iterations), so
  • it never has a null value, and
  • When a record is updated, its value is constantly automatically incremented.
  • Likewise, an optimistic concurrency token is automatically configured for the property.

At this point, updating aCar Two update statements are displayed:

DECLARE @p int
UPDATE [dbo].[Product]
SET @p = 0
WHERE (([Id] = @0) AND ([Rowversion] = @1))
SELECT [Rowversion]
FROM [dbo].[Product]
WHERE @@ROWCOUNT > 0 AND [Id] = @0

UPDATE [dbo].[Car]
SET ...

The first statement increments the rowversion rather than updating anything, and it will raise a concurrency exception if the rowversion was modified in between.

The [System.ComponentModel.DataAnnotations.Schema.Timestamp] attribute is comparable to in data annotations.IsRowVersion() :

[Timestamp]
public byte[] RowVersion { get; set; }
22
11/25/2017 10:38:03 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow