Pluc
Pluc

Reputation: 2929

IsConcurrencyToken in the fluent API is not affecting the migration scaffolding

This looks really simple and dumb, but it's not working for some reason... I got this line in my DbContext's OnModelCreating method :

modelBuilder.Entity<Client>().Property(o => o.RowVersion).IsConcurrencyToken();

And I got this line in the Client entity :

public byte[] RowVersion { get; set; }

And the scaffolded migration ends up like this :

AddColumn("dbo.Client", "RowVersion", c => c.Binary());

If I add the attribute [Timestamp] to the property, then it works and gives me :

AlterColumn("dbo.Client", "RowVersion", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));

Is there something I'm missing here? I'm really confused... Theres nothing more to do in the tutorial.

-Edit-

Adding .IsRowVersion() with the fluent API made it work. Is the tutorial from Microsoft simply wrong or is that a breaking change without EF6 versions? (I'm on 6.1.1 I believe)

Upvotes: 1

Views: 870

Answers (2)

Sajjad Mahmood
Sajjad Mahmood

Reputation: 21

IsConcurrencyToken does not work for byte[]

the data type is nvarchar(max) its value is always null if you don't initialize it its value is not auto-incremented when a record is updated.

IsRowVersion on the other hand, has datatype rowversion, so its value is never null, and its value is always auto-incremented when a record is updated. and it automatically configures the property to be an optimistic concurrency token.

Upvotes: 2

Pluc
Pluc

Reputation: 2929

Adding .IsRowVersion() with the fluent API made it work.

Is the tutorial from Microsoft simply wrong or is that a breaking change without EF6 versions? (I'm on 6.1.1 I believe) If someone can answer this, I will gladly change the accepted answer to you more detailed answer.

Upvotes: 2

Related Questions