带有Microsoft Identity 2的用户首选项表

时间:2018-02-21 00:01:09

标签: c# asp.net-mvc entity-framework asp.net-identity

我正在尝试使用Survey MVC Web应用程序的Microsoft Identity 2将用户首选项扩展和存储到数据库中。

目前,我只想存储用户的信息中心问题类型视图(1到5星,或0到10个NPS系统)。

我创建了一个具有以下属性的表AspNetUserPreferences:

Id (int)
UserId (nvarchar(128))
DashboardQuestionType (int)

模特:

[Table("AspNetUserPreferences")]
public class UserPreference
{
    [Key]
    public int Id { get; set; }
    public string UserId { get; set; }

    [ForeignKey("UserId")]
    public ApplicationUser User { get; set; }
    public QuestionType DashboardQuestionType { get; set; }
}

public enum QuestionType
{
    [Display(Name = "Enum_Type_Numeric_1_to_5_Display", ResourceType = typeof(ResQuestion))]
    Numeric1to5 = 1,
    [Display(Name = "Enum_Type_Numeric_0_to_10_Display", ResourceType = typeof(ResQuestion))]
    Numeric0to10 = 2,
    [Display(Name = "Enum_Type_Text_Display", ResourceType = typeof(ResQuestion))]
    Text = 3,
    [Display(Name = "Enum_Type_Yes_No_Display", ResourceType = typeof(ResQuestion))]
    YesNo = 6
}

上下文:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here
        userIdentity.AddClaim(new Claim("Company_Id", this.Company_Id.ToString()));
        userIdentity.AddClaim(new Claim("PTLocationDistrict", this.PTLocationDistrict.ToString()));
        userIdentity.AddClaim(new Claim("Location_Id", this.Location_Id.ToString()));

        return userIdentity;
    }

    // Additional properties for the Application User

    public string FullName { get; set; }

    public virtual int Company_Id { get; set; }

    [ForeignKey("Company_Id")]
    public virtual Company Company { get; set; }

    public PTLocationDistrict PTLocationDistrict { get; set; }

    public virtual int? Location_Id { get; set; }

    [ForeignKey("Location_Id")]
    public virtual Location Location { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("SurveyDB", throwIfV1Schema: false) { }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

当我尝试运行应用程序时,它根本不会从数据库中返回任何数据,并生成错误:

EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

1 个答案:

答案 0 :(得分:1)

Microsoft身份实现使用映射到表AspNetUsers,AspNetUserLogins等的IdentityUser,IdentityUserLogin等,由于在默认情况下在后台进行了处理,因此这些方法通常工作良好。当添加与身份表相关的新表(例如,UserId FK nvarchar(128))时,除非您明确告诉映射器,否则映射器不会知道此信息,因此会出现错误消息(您的新表不在默认上下文中,因此无法找到主要的),ApplicationDbContext代码中只有构造函数初始化,因此在其中添加DbSet <> AspNetUserPreferences和一个OnModelCreating()方法(定义缺少的键或对所有表进行反向工程并创建新的上下文)可能就足够了。需要在Models文件夹中定义一个AspNetUserPreference.cs类,并在上下文类中添加以下其他代码(可以进行修改以适合您的类)以进行连接。

public virtual DBSet<AspNetUserPreference> AspNetUserPreferences { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
        modelBuilder.Entity<AspNetUserPreferences>(entity =>
        {
            entity.HasIndex(e => e.NormalizedName)
                .HasName("RoleNameIndex")
                .IsUnique()
                .HasFilter("([NormalizedName] IS NOT NULL)");

            entity.Property(e => e.Id).ValueGeneratedNever();
        });