Autoincrement not working in EF Core for custom type primary key

Viewed 63

How can I use SQL Server auto-incremented primary key but with my custom type as PK?

Everything is working correctly, but when I'm creating a new entity it's Id=0 in the database, and isn't incrementing :(

// Custom PK type
public record struct IdentityId(long Value);

// Value converter for custom PK type
public class IdentityIdConverter : ValueConverter<IdentityId, long>
{
    public IdentityIdConverter() : base(
        id => id.Value,
        value => new IdentityId(value)
    ) {}
}

// Register converter in DbContext
...
protected override void ConfigureConventions(ModelConfigurationBuilder cb)
{
    base.ConfigureConventions(cb);
    cb.Properties<IdentityId>().HaveConversion<IdentityIdConverter>();
}
...

And when I try to add the generation manually, the following exception occurs:

builder.Property(e => e.Id).ValueGeneratedOnAdd();

Exception:

The property 'AppUser.Id' does not have a value set and no value generator is available for properties of type 'IdentityId'. Either set a value for the property before adding the entity or configure a value generator for properties of type 'IdentityId' in 'OnModelCreating'

0 Answers
Related