Unable to track an entity of type because primary key property 'id' is null [duplicate]
Asked Answered
R

3

30

After I upgraded to Asp.Net Core 3.0 I am getting the following error for my Identity class when attempting to create a user:

Unable to track an entity 'User' of type because primary key property 'id' is null.

Here User represents my extension of the Identity model.

This is the line of code where the error occurs:

var result = await _userManager.CreateAsync(user, password);
Recombination answered 2/12, 2019 at 7:33 Comment(4)
Does this answer your question? UserManager throws exception - Unable to track an entity because primary key property 'Id' is null - after upgrading from .Net Core 2.2 to 3.0Dumbwaiter
I prefer the formatting of your question, but the linked question is most definitely a duplicate. This question is definitely going to come back as more people migrate so maybe a gold badge holder can decide which becomes the surviving question?Dumbwaiter
Thanks for letting me know! When I stumbled across this error I wasn’t able to find the thread you mentioned. @AFriendRecombination
Agreed! I can see a lot of People facing this issue as they migrate to Core 3.0 @AFriendRecombination
R
69

This is due to breaking changes in EF Core 3.0 which can be found under this link:

https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#string-and-byte-array-keys-are-not-client-generated-by-default

The problem can be solved by assigning a key manually like this:

// assign GUID to Id
user.Id = Guid.NewGuid().ToString();
var result = await _userManager.CreateAsync(user, password);

Another solution would be the following according to Microsoft:

The pre-3.0 behavior can be obtained by explicitly specifying that the key properties should use generated values if no other non-null value is set. For example, with the fluent API:

modelBuilder
    .Entity<Blog>()
    .Property(e => e.Id)
    .ValueGeneratedOnAdd();

Or with data annotations:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
Recombination answered 2/12, 2019 at 7:33 Comment(0)
F
14

This solved the issue for me.

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        var keysProperties = builder.Model.GetEntityTypes().Select(x => x.FindPrimaryKey()).SelectMany(x => x.Properties);
        foreach (var property in keysProperties)
        {
            property.ValueGenerated = ValueGenerated.OnAdd;
        }
    }

Fielder answered 18/12, 2019 at 10:46 Comment(1)
THANK YOU! perfect solution!Yourself
D
1

Another reason that can cause this error.

Remove such code (if exists).

entity.Property(e => e.Id).ValueGeneratedNever();

This code is written automatically if you scaffold your existing database (and the column Id was IsIdentity = false) to domain classes.

Distinction answered 28/11, 2022 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.