简体   繁体   中英

Users property from IdentityRole moved or deleted on ASP.NET 2.0

In the previous version I was able to feed my model the number of Users from the Identity package, specifically IdentityRole:

        model = roleManager.Roles.Select(r => new ApplicationRoleListviewModel
        {
            RoleName = r.Name,
            Id = r.Id,
            Description = r.Description,
            NumberOfUsers = r.Users.Count
        }).ToList();

Now there is an error mentioning that Users is not defined. Was this property deleted or moved?

Name and Id is extracted from:

namespace Microsoft.AspNetCore.Identity
{
    public class IdentityRole<TKey> where TKey : IEquatable<TKey>
  {
    public IdentityRole();
    public IdentityRole(string roleName);
    public virtual TKey Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string NormalizedName { get; set; }
    public virtual string ConcurrencyStamp { get; set; }
    public override string ToString();
  }
}

Description property is a property defined by me on this project.

Thanks

Since the navigation properties were removed by Microsoft, you're going to have to put them back in yourself. The easiest way to do that would be to create your own role class, for example:

public class ApplicationRole : IdentityRole
{
    public virtual ICollection<User> Users { get; } = new List<Users>();
}

And then change your context to use this class instead of the default.

Although it doesn't answer your specific IdentityRole problem, there is a section in Migrating Authentication and Identity to ASP.NET Core 2.0 that states:

The Entity Framework Core navigation properties of the base IdentityUser POCO (Plain Old CLR Object) have been removed.

Given that the latest version of IdentityRole does not have the Users navigation property as you pointed out, it looks like you would need to add it back manually if you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM