npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.59k stars 227 forks source link

UseHilo For Not PK Property #2584

Open muratyuceer opened 1 year ago

muratyuceer commented 1 year ago

I am trying generate AccountNumber with hilo but it is not working.

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
modelBuilder.HasSequence<int>("Account_AccountNo", CoreDbProperties.DbSchema).IncrementsBy(100);
}

public class AccountConfiguration : IEntityTypeConfiguration<Account>
{
public void Configure(EntityTypeBuilder<Account> builder)
    {
        builder
            .Property(x => x.AccountNo)
            .UseHilo("Account_AccountNo",CoreDbProperties.DbSchema);
    }
}

    var account = new Data.Entities.Account
        {
            Id = Guid.NewGuid()
        };

        await _coreDbContext.Accounts.AddAsync(account);
//accountNo still 0 here
roji commented 1 year ago

@muratyuceer sorry for not answering this sooner - this slipped under my radar.

HiLo cannot be used with Guids; the point is that ID values are taken from a database sequence, and sequences only support numbers.

As this question is a bit old I'll go ahead and close this issue, but I'll be happy to help further and explain if needed.

muratyuceer commented 1 year ago

As this question is a bit old I'll go ahead and close this issue, but I'll be happy to help further and explain if needed.

Hello roji, actually AccountNo was integer, Id is Guid (PK) but AccountNo field is not the primary key. My question was about AccountNo field not Id field.

CheRrik commented 1 year ago

As this question is a bit old I'll go ahead and close this issue, but I'll be happy to help further and explain if needed.

Hello roji, actually AccountNo was integer, Id is Guid (PK) but AccountNo field is not the primary key. My question was about AccountNo field not Id field.

@muratyuceer add builder.HasAlternateKey(q => q.AccountNo); to your entity configuration, it should fix the issue.

muratyuceer commented 1 year ago

As this question is a bit old I'll go ahead and close this issue, but I'll be happy to help further and explain if needed.

Hello roji, actually AccountNo was integer, Id is Guid (PK) but AccountNo field is not the primary key. My question was about AccountNo field not Id field.

@muratyuceer add builder.HasAlternateKey(q => q.AccountNo); to your entity configuration, it should fix the issue.

Thank you for the response, but I won't be able to try it; it's been a long time since I submitted that project :)

roji commented 1 year ago

@CheRrik @muratyuceer you definitely shuoldn't need to define something as a key just in order to use HiLo with it.

Can you please try (without HasAlternateKey) with the latest 8.0.0 preview (preview7 currently, rc1 comes out in around a week)? HiLo was intentionally limited to key-only properties previously, but this limitation has been lifted in EF Core 8.0 (https://github.com/dotnet/efcore/issues/29758).