Entity Framework Core IdentityUser IndentityDbContext. 'IdentityUserClaim<string>' with foreign key properties is not compatible

0

I started using Entity Framework Core 2.1 and the IdentityUser. I did database first with the command line scaffold command.

public class Users: IdentityUser

In startup.cs

 public void ConfigureServices(IServiceCollection services)
 {    
     services.AddIdentity<Users, IdentityRole>(cfg =>
     {
         cfg.User.RequireUniqueEmail = true;
     })
     .AddEntityFrameworkStores<MyDbContext>();    
}

In MyDbContext.cs, I have this:

public class MyDbContext : IdentityDbContext<Users>  
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Users>(entity =>
        {
            entity.HasKey(e => e.AccountId);

            entity.ToTable("Users");
        }
    }    
}

When my code called the following

var user = await userManager.FindByNameAsync(model.Username);

I get the following error:

System.InvalidOperationException: The relationship from 'IdentityUserClaim' to 'Users' with foreign key properties {'UserId' : string} cannot target the primary key {'AccountId' : int} because it is not compatible. Configure a principal key or...

In the Users table there is a property:

public int AccountId { get; set; }

I tried a few test

  1. I added this property to the Users entity class:

    public string UserId { get; set; }
    
  2. In the OnModelingCreating method, I changed the code to in MyDbContext.cs from

    entity.HasKey(e => e.AccountId); 
    

    to

    entity.HasKey(e => e.UserId);
    

but I get this error

System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'NormalizedUserName'.
Invalid column name 'UserId'.
Invalid column name 'AccessFailedCount'.
Invalid column name 'ConcurrencyStamp'.
Invalid column name 'EmailConfirmed'.
Invalid col...

Any help is appreciated.

asp.net-mvc
entity-framework-core-2.1
asked on Stack Overflow Sep 10, 2018 by user1250264 • edited Sep 10, 2018 by user1250264

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0