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

[Question] Redeclare IdentityUser.Id #27373

Closed
lonix1 opened this issue Feb 4, 2022 · 2 comments
Closed

[Question] Redeclare IdentityUser.Id #27373

lonix1 opened this issue Feb 4, 2022 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@lonix1
Copy link

lonix1 commented Feb 4, 2022

I checked StackOverflow first, then this repo's issues (the best I could find was this, but it's unclear what the solution is). I'm unsure whether this is an ASP.NET Core Identity issue or an EF Core issue. Please move it if this if the wrong repo?

Ask a question

My user class:

public class User : IdentityUser<long>
{
  //public virtual TKey Id { get; set; }              // as defined in base class
  public new Id Id => new Id(base.Id);                // <--- the problem
}

The Id type is a DDD Value Object which I use to prevent various bugs. I still want the underlying column to be long, I don't want to change anything database-related.

I could avoid the problem by changing the member signature, but it's more convenient than IdValueObject, IdV2, IdOther, etc.

My config, which works:

entityTypeBuilder.Property(o => o.Id).UseIdentityByDefaultColumn();

But when I add that problematic property, that line of config throws at startup:

'System.ArgumentException' in Npgsql.EntityFrameworkCore.PostgreSQL.dll: 'Identity value generation cannot be used for the property 'Id' on entity type 'User' because the property type is 'Id'. Identity columns can only be of type short, int or long.'

Which is understandable.

I realise I could turn off the automatic EF functionality and handle that class' PK myself, but that feels complicated. Is there some magic config option (EF has so many, it's easy to lose track!) to tell EF's configuration to use base.Id instead of Id?

Include your code

As above.

Include stack traces

Exception thrown: 'System.ArgumentException' in Npgsql.EntityFrameworkCore.PostgreSQL.dll: 'Identity value generation cannot be used for the property 'Id' on entity type 'User' because the property type is 'Id'. Identity columns can only be of type short, int or long.'
   at Microsoft.EntityFrameworkCore.NpgsqlPropertyExtensions.CheckValueGenerationStrategy(IProperty property, Nullable`1 value)
   at Microsoft.EntityFrameworkCore.NpgsqlPropertyExtensions.SetValueGenerationStrategy(IMutableProperty property, Nullable`1 value)
   at Microsoft.EntityFrameworkCore.NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(PropertyBuilder propertyBuilder)
   at Microsoft.EntityFrameworkCore.NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn[TProperty](PropertyBuilder`1 propertyBuilder)
   at MyProject.Context.OnModelCreating(ModelBuilder modelBuilder)

Include verbose output

n/a

Include provider and version information

EF Core version: 5.0.11
Database provider: Npgsql 5.0.10
Target framework: .NET 5.0
Operating system: linux
IDE: vscode 1.63.2

Final thoughts

Apologies if this is out-of-scope. It's just that we're "all in" on DDD, and I'm sure there's some way to get EF and DDD to work nicely together! Any tips appreciated.

@ajcvickers
Copy link
Member

@lonix1 Please try EF Core 6.0. We made some improvements in this area in 6.0.

@lonix1
Copy link
Author

lonix1 commented Feb 4, 2022

I'm unsure when we'll do the 5 -> 6 upgrade, it's a big undertaking, but it's on the backlog.

I'll close for now, and reply at some point in the future. Thanks!

@lonix1 lonix1 closed this as completed Feb 4, 2022
@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Feb 4, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants