Last active
October 17, 2017 20:03
-
-
Save sverrehundeide/6734ea54abb7cb2446d7eafc3852b588 to your computer and use it in GitHub Desktop.
Blog - Optimistic concurrency with MySQL and Entity Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Property(x => x.Version).IsRequired() | |
.IsConcurrencyToken() | |
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var concurrencyTokenEntries = ChangeTracker.Entries<IVersionedEntity>(); | |
foreach (var entry in concurrencyTokenEntries) | |
{ | |
if (entry.State == EntityState.Unchanged) | |
{ | |
continue; | |
} | |
entry.Entity.Version = Guid.NewGuid(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TIMESTAMP type with default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Property(x => x.Version).IsRequired() | |
.IsConcurrencyToken() | |
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public System.Guid Version { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IVersionedEntity | |
{ | |
Guid Version { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read full blog post at https://blog.hundeide.net/2015/05/optimistic-concurrency-with-mysql-and-entity-framework/