3

We have a table that has a column called Version that is mapped as a SQL rowversion. This is done because we have an external system that maps to our data that relies on this column changing every time the table is updated. Originally we wanted this to be handled by SQL, but now we are finding we have Optimistic Concurrency exceptions. While these exceptions are there to safeguard data getting overwritten, for our case we do not care about this. We simply want the timestamp to continue incrementing, without Entity Framework checking it for concurrency issues.

Is there any way to do this?

Mapping:

Property(t => t.Version).IsRowVersion();
4
  • 1
    Yes, just remove it from your mapping. Sql Server will increment it on each update anyway. Commented May 16, 2017 at 18:49
  • I tried removing it from my mapping but i get an error: cannot update timestamp column Commented May 17, 2017 at 13:41
  • Did you actively mark the property as ignored? On second thoughts, why not remove it from your class model altogether? Commented May 17, 2017 at 13:45
  • We need to be able to use the column in our app as we need to provide the value to another app that uses our api, so removing it from the model is out of the question. The answer below has resolved our problem. Commented May 17, 2017 at 14:05

2 Answers 2

8

By default EF implies row version type to be a optimistic concurrency token (included in the optimistic concurrency checks).

You can override that behavior by using the IsConcurrencyToken fluent API:

Property(t => t.Version).IsRowVersion().IsConcurrencyToken(false);
Sign up to request clarification or add additional context in comments.

2 Comments

I'm going to test this now, if this works I could kiss you
works! thank you so much! we've been trying to find the source of these concurrency issues for months
0

In EF core adding IsConcurrencyToken(false) will change the column type from rowversion to varbinary(max).

Instead you should replace IsRowVersion() with HasColumnType("rowversion") like this:

Property(e => e.LastUpdatedRowVer).HasColumnType("rowversion").ValueGeneratedOnAddOrUpdate();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.