Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddEntityFrameworkStores can only be called with a role that derives from IdentityRole in .NET Core 2.0

I have changed a project from the .NET Core 1.1 to 2.0 version, but I'm getting an error from the Identity, when It tries to add the stores:

services.AddIdentity<ApplicationUser, IdentityRole<long>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

The thrown error is:

AddEntityFrameworkStores can only be called with a role that derives from IdentityRole

These are my classes:

public class ApplicationUser : IdentityUser<long>
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<long>, long>        
{
        public ApplicationDbContext(DbContextOptions options) : base(options) { 
        }
}

Someone could help me?

like image 572
Felipe Santana Avatar asked Aug 16 '17 20:08

Felipe Santana


People also ask

What is AddEntityFrameworkStores?

AddEntityFrameworkStores<TContext>(IdentityBuilder)Adds an Entity Framework implementation of identity information stores. C# Copy.

What is Identity in. net Core?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.


1 Answers

Long time since I asked this question, but here's how I deal with nowadays:

Startup.cs

services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<RoleManager<Role>>();

Entites:

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}
like image 65
Felipe Santana Avatar answered Sep 17 '22 17:09

Felipe Santana