针对ApplicationUser Roles导航属性的ASP MVC Build Throwing Warning?

我有以下ApplicationUser模型:

public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection Roles { get; set; } public bool HasRole(string _role) { return Roles.Any(r => r.Name == _role); } public bool HasPermission(string _permission) { return Roles.Any(r => r.Permissions .Any(p => p.Name == _permission)); } } 

但是当我运行构建时,我收到以下警告消息:

 ApplicationUser.Roles hides inherited member 'IdentityUser.Roles. To make the current member override that implementation, add the overide keyword. Otherwise add the new keyword. 

我的实现有问题还是应该以不同的方式完成? 我添加了Roles导航属性,以便我可以实现HasRole和HasPermission方法。

我的Permission和ApplicationRole模型实现如下:

 public class Permission { public byte Id { get; set; } public string Name { get; set; } public virtual List Roles { get; set; } } public class ApplicationRole : IdentityRole { public ApplicationRole() : base() { } public ApplicationRole(string name) : base(name) { } public virtual ICollection Permissions { get; set; } public bool IsPermissionInRole(string _permission) { return Permissions.Any(p => p.Name == _permission); } } 

我对ASP.NET身份没有广泛的了解。 但经过一番搜索,我可以给你粗略的回答。 IdentityUser应该具有inheritanceIdentityUserRole而非IdentityRole的 proeprty 角色 。 我认为这个模型与IdentityUsers和IdentityRoles有关。 那么,你应该做的是创建inheritanceIdentityUserRole的 ApplicationUserRole类:

 public class ApplicationUserRole : IdentityUserRole { public ApplicationUserRole() : base() { } public virtual ApplicationRole Role { get; set; } } 

IdentityRoleinheritance您的ApplicationRole IdentityRole

 public class ApplicationRole : IdentityRole { } 

然后在ApplicationUser类中使用此类。 要使用ApplicationUserRole,您需要从IdentityUserinheritanceApplicationUser IdentityUser不是IdentityUser

 public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } ............. } 

最后,将ApplicationUserHasPermission方法更改为:

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

我再说一遍,这是粗略的回答。 有关扩展Identity模型的更多信息,请参阅此代码项目文章 。