2
votes

I'm working with a bounch of entities each of them having several properties. Each property is involved in optimistic concurrency check on saving. I prefer avoid using of timestamp as I should add the field on each tables, Im working with a legacy database and I don't want to make any changes on it. The solution adopted rely on calling IsConcurrencyToken in the modelBuilder

 protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<MyEntity1>(entity =>
                {
                    entity.Property(e => e.Property1).IsConcurrencyToken;
                    entity.Property(e => e.Property2).IsConcurrencyToken;
                    entity.Property(e => e.Property3).IsConcurrencyToken;


                    entity.Property(e => e.PropertyN).IsConcurrencyToken;
                });         
             }

but do I really need to explicitly call IsConcurrencyToken for every properties? Is there any chance to configure my entities to automatically include every properties in the concurrency check?

2
but do I really need to set call IsConcurrencyToken for every properties? That's primarily opinion-based. - Gert Arnold
@GertArnold - no it is not... It is a technical question based on precise enough requirements. - Henk Holterman
Im not asking opinion about the choice to include all properties in the concurrency check, but if there is a faster way than explicitly set it property by property to do it. - Ghini Antonio
It is opinion as long as we don't have more details. What about fields of which the current db value doesn't matter at update, or fields that won't ever be updated after create (as per business logic)? What about computed fields of which the value changed while editing? No need to mark them as concurrency token. And then there may be fields that should have a "last one wins" strategy (f.e. "LastSeenBy"). IMO marking all properties as concurrency token is too simple. It may have to be a per-entity operation. - Gert Arnold

2 Answers

4
votes

Is there any chance to configure my entities to automatically include every properties in the concurrency check?

Yes. You can iterate over all your entities and properties in OnModelCreating:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    foreach (var entity in modelBuilder.Model.GetEntityTypes())
    {
        foreach (var prop in entity.GetProperties())
        {
            prop.IsConcurrencyToken = true;
        }
    }

    base.OnModelCreating(modelBuilder);
}
0
votes

Thanks to David Browne for the suggestion.

As I want to be able to enable concurrency entity by entity I wrapped the code in extension method

  public static EntityTypeBuilder<TEntity> EnableConcurrency<TEntity>(this EntityTypeBuilder<TEntity> entityTypeBuilder) where TEntity : class
        {
            var entity = entityTypeBuilder.Metadata.Model.FindEntityType(typeof(TEntity));
            foreach (var prop in entity.GetProperties())
            {
                prop.IsConcurrencyToken = true;
            }
            return entityTypeBuilder;
        }

and Im going to use it like that

modelBuilder.Entity<MyEntity>(entity =>
            {
                entity.EnableConcurrency();
            });