添加自定义UserRole get时的Microsoft.AspNet.Identity不能用作泛型类型或方法中的类型参数“TUser”

问题描述 投票:0回答:2

我使用 Microsoft.AspNet.Identity.EntityFramework 并需要自定义 Identity 类属性。

我的上下文如下:

 public class GSSDbContext : IdentityDbContext<ApplicationUser, AppRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> 
    {
        public static string ConnectionString { get; set; }
        private static GSSDbContext _context;

        static GSSDbContext()
        {
#if DEBUG
            ConnectionString = "GssConnection";
#endif
        }
}

我的身份类别是:

 public class ApplicationUser: IdentityUser 
    {
        public int Loc_Id { get; set; }
        [ForeignKey("Loc_Id")]
    
        public virtual Location Location { get; set; }
       
        public virtual Person Person { get; set; }
       }
 public class AppRole : IdentityRole
    
    {
        public string PersianName { get; set; }
        public int Type { get; set; }
}

一切都很好,直到我为 IdentityUserRole 添加自定义类并更改我的代码,例如:

public class GSSDbContext : IdentityDbContext<ApplicationUser, AppRole, string, IdentityUserLogin, AppUserRole, IdentityUserClaim> 
    {
        public static string ConnectionString { get; set; }
        private static GSSDbContext _context;

        static GSSDbContext()
        {
#if DEBUG
            ConnectionString = "GssConnection";
#endif
        }
}
}

public class AppUserRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole
    {
       
        public string name { get; set; }
        [Timestamp]
        public byte[] RowVersion { get; set; }
    }

并出现错误:

严重性代码描述项目文件行抑制状态

错误 CS0311 该类型不能用作泛型类型或方法“IdentityDbContext”中的类型参数“TUser”。没有隐式引用

我该如何修复它?

c# asp.net-mvc entity-framework asp.net-identity
2个回答
0
投票

将代码更改为: 背景:

public class GSSDbContext : IdentityDbContext<ApplicationUser, AppRole, string, IdentityUserLogin, AppUserRole, IdentityUserClaim> 
    {
        public static string ConnectionString { get; set; }
        private static GSSDbContext _context;

        static GSSDbContext()
        {
#if DEBUG
            ConnectionString = "GssConnection";
#endif
        }
}
 public class ApplicationUser : IdentityUser<string, IdentityUserLogin, AppUserRole, IdentityUserClaim>
AppRole : IdentityRole<string, AppUserRole>{}

public class AppRole : IdentityRole<string, AppUserRole>{}

问题就解决了。 实际上,我必须定义新的 IdentityUserRole (名称:appUserRole) 对于我的用户和角色类。关键是:

身份用户


-1
投票

同时使用

IdentityUser
IdentityRole
时,您必须指定主键类型,这是为了确保它们之间的链接表具有正确的外键类型。

IdentityContext
的第三个泛型参数中,您指定了“字符串”类型,这是
TKey
泛型参数。您必须告诉
IdentityUser
IdentityRole
使用“字符串”作为其主键。

您可以通过更新两种类型的派生实现以将通用

TKey
传递到父级来实现此目的。

public class ApplicationUser : IdentityRole<string>
public class AppRole : IdentityRole<string>
© www.soinside.com 2019 - 2024. All rights reserved.