I'm working on my project it's based on ASP.net Core 2.0. I've created ApplicationRole
andApplicationUser
class. I've wrote a snippet code for making roles when I run my project I've got this error:
System.Data.SqlClient.SqlException: 'Invalid column name 'ApplicationRoleId'.
But the same code that I use in ASP.net Core 1.1 didn't have any problem.
I think it has a problem with this line of code:
lineNumberOfUsers = r.Users.Count
Because when I comment this code project works fine, but I need count of users for every role.
Controller:
publicclassApplicationRoleController:Controller{privatereadonlyUserManager<ApplicationUser> _userManager;privatereadonlyRoleManager<ApplicationRole> _roleManager;publicApplicationRoleController(UserManager<ApplicationUser> userManager,RoleManager<ApplicationRole> roleManager){
_userManager = userManager;
_roleManager = roleManager;}publicIActionResultIndex(){List<ApplicationRoleViewModel> models =newList<ApplicationRoleViewModel>();
models = _roleManager.Roles.Select(r =>newApplicationRoleViewModel{Id= r.Id,Name= r.Name,Description= r.Description,NumberOfUsers= r.Users.Count}).ToList();returnView(models);}}
Model
publicclassApplicationRole:IdentityRole{publicstringDescription{ get;set;}publicvirtualICollection<ApplicationUser>Users{ get;}=newList<ApplicationUser>();}
I've checked sql query in sql server profiler. when i ran sql query in sql server I got the same error.
Invalid column name 'ApplicationRoleId'.
SELECT [r].[Id],[r].[Name],[r].[Description],(
SELECT COUNT(*)
FROM [AspNetUsers] AS [a]
WHERE [r].[Id]=[a].[ApplicationRoleId]) AS [NumberOfUsers]
FROM [AspNetRoles] AS [r]