ASP MVC Build Throwing Warning For ApplicationUser Roles Navigation Property?

Deadly 提交于 2019-11-29 12:13:37

I do not have broad knowledge about ASP.NET Identity. But after a little search I can give you rough answer. IdentityUser should have proeprty Roles which inherits IdentityUserRole not IdentityRole. I think this model relates IdentityUsers and IdentityRoles. So, what you should do is create ApplicationUserRole class which inherits IdentityUserRole:

public class ApplicationUserRole : IdentityUserRole
{
    public ApplicationUserRole()
        : base()
    { }

    public virtual ApplicationRole Role { get; set; }
}

Inherit your ApplicationRole from IdentityRole<string, ApplicationUserRole>:

public class ApplicationRole 
    : IdentityRole<string, ApplicationUserRole>
{
}

Then use this class in your ApplicationUser class. To use ApplicationUserRole you need to inherit ApplicationUser from IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim> isntead of IdentityUser

public class ApplicationUser 
    : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    .............
}

Finally, change your HasPermission method of ApplicationUser to something like:

public bool HasPermission(string _permission)
{
    return Roles.Any(r => r.Role.IsPermissionInRole(_permission));
}

I am stating again, this is rough answer. For more information about extending Identity models, please refer to this code project article.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!