I recently decided to implement the ASP.NET Identity functionality, on a website I'm developing in ASP.NET Core MVC.
Let's take a quick look at the tables and classes in topic:
public class User : IdentityUser
{
public string FirstName { get; set; }
public stirng LastName { get; set; }
public int CountryId { get; set; }
public Country Country { get; set; }
}
public class Country
{
public int Id { get; set; }
public string ISO { get; set; }
public string Name { get; set; }
}
The Country
class is somewhat irrelevant, but in case you wanted the reference from the User
class.
With this setup, I've configured my database context like this:
public class DatabaseContext : IdentityDbContext<User>
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<IdentityUser>.ToTable("User");
builder.Entity<IndentityRole>.ToTable("Roles");
builder.Entity<IdentityUserRole<string>>.ToTable("UserRoles");
builder.Entity<IdentityUserClaim<string>>.ToTable("UserClaims");
builder.Entity<IdentityUserLogin<string>>.ToTable("UserLogins");
}
}
I'm using custom tables (renamed) and mapping them like this.
Problem
Now, whenever I run my project, at the first database call I'm making, I'm getting the following error:
The entity type 'User' should derive from 'IdentityUser' to reflect the hierarchy of the corresponding CLR types.
To me, this error says that my class User
doesn't inherit from the interface IdentityUser
, and I'm not quite sure why that is so? Clearly my class User
DOES inherit from IdentityUser
?
Instead of
builder.Entity<IdentityUser>.ToTable("User");
You need to put
builder.Entity<User>.ToTable("Users");
And in the Startup class where you add the identity service you need to do this
services.AddIdentity<User, IdentityRole>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With