-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
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:
-
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.
-
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.