表映射ASPNET标识时出错

时间:2014-02-03 16:26:24

标签: asp.net entity-framework entity-framework-5 code-first

我很明显这是新手,但我一直在与新的身份会员提供商合作,试图将我们的旧网站转换为标准平台。到目前为止,所有内容似乎都按预期工作,但是当我尝试登录我们的网站时,我收到以下错误{“无效的列名'ApplicationUser_Id'。”}。我相信这与EF试图映射我的两个用户表之间的关系的方式有关。我有第一个表AspNetUsers,然后是一个名为ISF_ProfileEmployee的扩展配置文件表。请参阅以下信息。我正在关注pranav rastogi的博客http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx,所以如果他的帮助将非常感谢。

继承我的代码。

    public class ISF_ProfileEmployee
    {

        public Guid UserID { get; set; }
        public string AlternateEmployeeId { get; set; }
        public Guid DefaultDashboardId { get; set; }
        public DateTime LastUpdatedDate { get; set; }
        public string AlternateEmailAddress { get; set; }
        public DateTime EmploymentStartDate { get; set; }
        public string EmployeeStatus { get; set; }
        public bool IsSupervisor { get; set; }
        public Guid ISFLocationId { get; set; }
        public Guid ISFDepartmentId { get; set; }
        public Guid ISFSupervisorId { get; set; }
        public Guid ISFPositionId { get; set; }
        public Guid StatusId { get; set; }
        public string StatusMessage { get; set; }
        public DateTime StatusUpdateDate { get; set; }
        public bool InBoardUser { get; set; }
        public Guid? InBoardStatus { get; set; }
        [Required]
        public DateTime HireDate { get; set; }
        public string GersUserId { get; set; }
        public bool UseCustomDash { get; set; }
    }

    public class ApplicationUser : IdentityUser
    {
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string Nickname { get; set; }
        [Required]
        public string Email { get; set; }
        public bool Disabled { get; set; }
        public DateTime? BirthDay {get; set;}
        public string ImageFileName {get; set;}
        public string OfficePhone {get; set;}
        public string MobilePhone { get; set; }
        public string Extension { get; set; }
        public string FaxPhone {get; set;}
        public string PasswordQuestion {get; set;}
        public string PasswordAnswer {get; set;}
        public DateTime CreateDate {get; set;}
        public DateTime LastLockoutDate {get; set;}
        public bool IsLockedOut {get; set;}
        public int FailedPasswordAttemptCount {get; set;}
        public DateTime FailedPasswordAttemptWindowStart {get; set;}
        public int FailedPasswordAnswerAttemptCount {get; set;}
        public DateTime FailedPasswordAnswerAttemptWindowStart {get; set;}
        public DateTime LastPasswordChangedDate {get; set;}
        public string Comment {get; set;}

        public ISF_ProfileEmployee ISF_ProfileEmployee { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().HasKey(k => k.Id).Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<IdentityRole>().HasKey(r=>r.Id).Property(p => p.Id).HasColumnName("RoleId");
        modelBuilder.Entity<IdentityRole>().Property(p => p.Name).HasColumnName("RoleName");
        modelBuilder.Entity<ISF_ProfileEmployee>().ToTable("ISF_ProfileEmployee").HasKey(p => p.UserID);
        modelBuilder.Entity<ApplicationUser>().HasRequired(x => x.ISF_ProfileEmployee).WithRequiredPrincipal();

    }

2 个答案:

答案 0 :(得分:1)

我永远无法得到解决这个问题的答案。身份成员资格显然对您使用的表中的列名非常挑剔。我最终合并了我的两个表并将UserId列更改回Id(这是Aspnet Identity期望的名称)一旦我这样做了,我就能很容易地向前推进。在我看来,需要做更多的工作来定制它使用的模式。此外,缺乏文档非常令人沮丧,这个想法很棒,但是后续工作还有待改进。

答案 1 :(得分:0)

我认为可能发生的情况是,由于您从具有ApplicationUserId列的早期成员资格系统迁移,因此该列仍然存在,或者EF正在查找该列并且未能映射该列。尝试将ApplicationUserId列映射到UserId并查看是否有效