Skip to content

Concerns about manual ID generation in VogenIdValueGenerator #990

@hakakou

Description

@hakakou

The class VogenIdValueGenerator is used for generating integer primary keys. It basically has this code:

  public override TId Next(EntityEntry entry)
  {
    TContext ctx = (TContext)entry.Context;
    DbSet<TEntityBase> entities = (DbSet<TEntityBase>)_matchPropertyGetter!.GetValue(ctx)!;
    var next = Math.Max(
        MaxFrom(entities.Local),
        MaxFrom(entities)) + 1;

    return TId.From(next);

    static int MaxFrom(IEnumerable<TEntityBase> es) =>
        es.Any() ? es.Max(e => e.Id.Value) : 0;
  }

There are two major issues with this approach:

  1. To calculate the next integer, the code uses IEnumerable and actually queries the entire database table, (all the columns and rows) which can be gigabytes of data, just to compute a single integer.

  2. Getting the next int in autoinc columns is actually very complicated and should be left to the database itself (there can be concurrency issues, locking, or special rules for replcation etc). For example in the implementations of EF Core, the INSERT typically does not inclue the ID column. The database returns the new Id which is set back in the entity class after the commit.

In general, I don't think it's possible to not use a primitive value for the ID columns. In my opinion this code should be removed and ID columns should have a primitive integer, or at least a Vogen non-primitive GUID.

If you agree, I can create a pull request with this change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions