Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Roles and RoleManager in ASP.NET Core 2

$
0
0

My next step in this journey is to create some roles and a simple user.  Unfortunately, I get an error regarding my tables not having primary keys as shown below.  I don't understand how to fix it.  I think this should be really simple, I should just get an instance of a role manager, and then just add a role, so I am really surprised by this.  Unfortunately, the error message is saying that the IdentityUserLogin<Guid> doesn't have a primary key.  When I look in OnModelCreate, there is a Key entry for that table, so I am not sure what is going on.  Any help is appreciated.

TIA


I am trying to use the following code in my Startup's ConfigureServices method:
// Build the intermediate service provider
var serviceProvider = services.BuildServiceProvider();

//resolve implementations
var dbContext = serviceProvider.GetService<PoopTheWorldContext>();
var _context = new PoopTheWorldContext(
serviceProvider.GetRequiredService<DbContextOptions<PoopTheWorldContext>>());
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
var roleManager = serviceProvider.GetService<RoleManager<ApplicationRole>>();
var roleTask = roleManager.RoleExistsAsync("Administrator");
var roleExists = roleTask.Result;

Exception:
System.AggregateException occurred
HResult=0x80131500
Message=One or more errors occurred. (The entity type 'IdentityUserLogin<Guid>' requires a primary key to be defined.)
Source=<Cannot evaluate the exception source>
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at PooperApp.Startup.ConfigureServices(IServiceCollection services) in C:\Users\wallym\Documents\Visual Studio 2017\Projects\PooperApp\PooperApp\Startup.cs:line 91

Inner Exception 1:
InvalidOperationException: The entity type 'IdentityUserLogin<Guid>' requires a primary key to be defined.

I check out my OnModelCreating, and I see a key definition for IdentityUserLogin.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AspNetRoleClaims>(entity =>
{
entity.HasIndex(e => e.RoleId);

entity.HasOne(d => d.Role)
.WithMany(p => p.AspNetRoleClaims)
.HasForeignKey(d => d.RoleId);
});

modelBuilder.Entity<AspNetRoles>(entity =>
{
entity.HasIndex(e => e.NormalizedName)
.HasName("RoleNameIndex")
.IsUnique()
.HasFilter("([NormalizedName] IS NOT NULL)");

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

entity.Property(e => e.Name).HasMaxLength(256);

entity.Property(e => e.NormalizedName).HasMaxLength(256);
});

modelBuilder.Entity<AspNetUserClaims>(entity =>
{
entity.HasIndex(e => e.UserId);

entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserClaims)
.HasForeignKey(d => d.UserId);
});

modelBuilder.Entity<AspNetUserLogins>(entity =>
{
entity.HasKey(e => new { e.LoginProvider, e.ProviderKey });
entity.HasIndex(e => e.UserId);

entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserLogins)
.HasForeignKey(d => d.UserId);
});

modelBuilder.Entity<AspNetUserRoles>(entity =>
{
entity.HasKey(e => new { e.UserId, e.RoleId });

entity.HasIndex(e => e.RoleId);

entity.HasOne(d => d.Role)
.WithMany(p => p.AspNetUserRoles)
.HasForeignKey(d => d.RoleId);

entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserRoles)
.HasForeignKey(d => d.UserId);
});

modelBuilder.Entity<AspNetUsers>(entity =>
{
entity.HasIndex(e => e.NormalizedEmail)
.HasName("EmailIndex");

entity.HasIndex(e => e.NormalizedUserName)
.HasName("UserNameIndex")
.IsUnique()
.HasFilter("([NormalizedUserName] IS NOT NULL)");

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

entity.Property(e => e.Email).HasMaxLength(256);

entity.Property(e => e.NormalizedEmail).HasMaxLength(256);

entity.Property(e => e.NormalizedUserName).HasMaxLength(256);

entity.Property(e => e.UserName).HasMaxLength(256);
});

modelBuilder.Entity<AspNetUserTokens>(entity =>
{
entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name });

entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserTokens)
.HasForeignKey(d => d.UserId);
});
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>