Hi, I am rather new to asp net core. working now in version 2.2.
With the Identity framework I try to create a site that allows only known users. So far I manege rather good, got role management working, and added some users in a Admin role. I And I can add new Users to the system. Now I want to create a razorpage that
list all users known to the system.
I added a new page in Areas-Identity-Account and called it UserList. this is my UserList.cshtml page:
@page
@model RoosterApp.Areas.Identity.Pages.UserListModel
@{
}
UserList Pagina
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Users[0].KLMId)
</th>
<th>
@Html.DisplayNameFor(model => model.Users[0].Name)
</th>
</tr>
</thead>
<tbody>
@foreach(var item in Model.Users)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.KLMId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.KLMId">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.KLMId">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.KLMId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
and this is my UserList.cshtml.cs file so far:
using Microsoft.AspNetCore.Mvc.RazorPages;
using RoosterApp.Data;
using RoosterApp.Models;
using System.Collections.Generic;
namespace RoosterApp.Areas.Identity.Pages
{
public class User
{
public string KLMId { get; set; }
public string Name { get; set; }
}
public class UserListModel : PageModel
{
//private readonly UserManager<ApplicationUser> _userManager;
public List<User> Users;
public void OnGet()
{
Users = new List<User>();
List<ApplicationUser> UserList;
var context = new ApplicationDbContext();
UserList = context.Users.UserList(); //this is not working!!
also UserList = context.Users.ToList(); is not working
foreach (ApplicationUser au in UserList)
{
User U = new User();
U.KLMId = au.klmId;
U.Name = au.NormalizedUserName;
Users.Add(U);
}
}
}
}
and this is my ApplicationDBcontext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options) { }
public ApplicationDbContext()
{
}
}
The problem is that I can not get a list of users out of the ApplicationDbContext. Also applicationUser or ApplicationRole are not giving me the possibility to get a list of users. If I use UserList = context.Users.ToList() the project
runs but I get a error message:
System.InvalidOperationException
HResult=0x80131509
Message=No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure
that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Please Help.