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
I added this property to the Users
entity class:
public string UserId { get; set; }
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.
User contributions licensed under CC BY-SA 3.0