Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseHilo For Not PK Property #2584

Open
muratyuceer opened this issue Dec 3, 2022 · 5 comments
Open

UseHilo For Not PK Property #2584

muratyuceer opened this issue Dec 3, 2022 · 5 comments
Milestone

Comments

@muratyuceer
Copy link

muratyuceer commented Dec 3, 2022

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
Copy link
Member

roji commented Mar 9, 2023

@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.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2023
@muratyuceer
Copy link
Author

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.

@roji roji reopened this Mar 9, 2023
@CheRrik
Copy link

CheRrik commented Sep 8, 2023

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
Copy link
Author

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
Copy link
Member

roji commented Sep 8, 2023

@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 (dotnet/efcore#29758).

@roji roji added this to the Backlog milestone Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants