【问题标题】:EF code first existing database - IdentityUser table issueEF 代码第一个现有数据库 - IdentityUser 表问题
【发布时间】:2013-12-26 17:09:13
【问题描述】:

我对自定义用户有疑问。 我有一个名为 Profiles 的表,用于我的 users 表(它不允许我使用 Users,但这是一个不同的问题!)我创建了这样的 dbcontext:

public partial class SkipstoneContext : IdentityDbContext<Profile>
{
    static SkipstoneContext()
    {
        Database.SetInitializer<SkipstoneContext>(null); // Existing data, do nothing
    }

    public SkipstoneContext()
        : base("DefaultConnection")
    {
    }

    // ...
    public DbSet<Company> Companies { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(model => model.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(model => model.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(model => new { model.RoleId, model.UserId });
        modelBuilder.Entity<UserSecret>().HasKey<string>(model => model.UserName);
    }
}

如您所见,我不必为 Profile 创建一个 DbSet,因为它是在继承的 IdentityDbContext 类中设置的 p>

我的个人资料类如下所示:

public partial class Profile : IdentityUser
{
    public Profile()
    {
        // ...
    }

    public string CompanyId { get; set; }        
    public string Title { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    // ...

    public virtual Company Company { get; set; }
    // ...
}

当我运行我的项目时,我收到一条错误提示

对象名称“dbo.IdentityUsers”无效。

如果我将表重命名为 IdentityUsers 并再次运行该项目,我会收到一条错误提示

无效的对象名称“dbo.Profiles”。

这就是令人困惑的地方。看来 EF 正在为用户寻找 2 个表,而不仅仅是一个。

谁能给我解释一下为什么?

更新 1

因此,为了解决这个问题,我从 SkipstoneContext 中删除了继承的 IdentityDbContent,而改为从 DbContext 继承。 新的上下文类如下所示:

public partial class SkipstoneContext : DbContext // : IdentityDbContext<Profile>
{
    static SkipstoneContext()
    {
        Database.SetInitializer<SkipstoneContext>(null); // Exsting database, do nothing
    }

    public SkipstoneContext()
        : base("DefaultConnection")
    {
    }

    // ...
    public DbSet<Company> Companies { get; set; }
    public DbSet<Profile> Profiles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // ...
        modelBuilder.Configurations.Add(new CompanyMap());
        modelBuilder.Configurations.Add(new ProfileMap());
        /// ...
        //modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(model => model.UserId);
        //modelBuilder.Entity<IdentityRole>().HasKey<string>(model => model.Id);
        //modelBuilder.Entity<IdentityUserRole>().HasKey(model => new { model.RoleId, model.UserId });
        //modelBuilder.Entity<UserSecret>().HasKey<string>(model => model.UserName);
    }
}

我还注释掉了我对 IdentityUserLoginIdentityRoleIdentityUserRoleUserSecret 的绑定,这就是开始工作了。

看看IdentityUserLogin

public class IdentityUserLogin
{
    public IdentityUserLogin();

    public virtual string LoginProvider { get; set; }
    public virtual string ProviderKey { get; set; }
    public virtual IdentityUser User { get; set; }
    public virtual string UserId { get; set; }
}

它有一个 IdentityUser 属性,我认为这是导致我的问题的原因。我知道 Profile 继承了 IdentityUser 但由于某种原因它似乎认为它在另一个表中。

因此,我的下一个任务是创建自定义 UserLogin 并查看是否可以让 EF 使用 IdentityUserLogin

的插入

【问题讨论】:

  • 您有DbSet 对应的ProfileIdentityUser 吗?
  • 不,这就是我要说的。 IdentityDbContext 应该处理我的用户表(我假设它会很乐意使用 Profiles,因为我的 User 类称为 Profile)但它似乎由于某种原因而倒下

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


【解决方案1】:

好的,我想通了。这是由于我的映射。我的 Profile 映射类如下所示:

public class ProfileMap : EntityTypeConfiguration<Profile>
{
    public IdentityUserMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Id)
            .IsRequired()
            .HasMaxLength(128);

        this.Property(t => t.CompanyId)
            .IsRequired()
            .HasMaxLength(128);

        this.Property(t => t.CreatedById)
            .IsRequired()
            .HasMaxLength(128);

        this.Property(t => t.ModifiedById)
            .HasMaxLength(128);

        this.Property(t => t.Title)
            .IsRequired();

        this.Property(t => t.Forename)
            .IsRequired();

        this.Property(t => t.Surname)
            .IsRequired();

        this.Property(t => t.Email)
            .IsRequired();

        this.Property(t => t.CredentialId)
            .IsRequired();

        this.Property(t => t.PasswordHash)
            .HasMaxLength(128);

        this.Property(t => t.SecurityStamp)
            .HasMaxLength(128);

        this.Property(t => t.Discriminator)
            .HasMaxLength(128);

        // Table & Column Mappings
        this.ToTable("Profiles");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.CompanyId).HasColumnName("CompanyId");
        this.Property(t => t.CreatedById).HasColumnName("CreatedById");
        this.Property(t => t.ModifiedById).HasColumnName("ModifiedById");
        this.Property(t => t.DateCreated).HasColumnName("DateCreated");
        this.Property(t => t.DateModified).HasColumnName("DateModified");
        this.Property(t => t.LastLoginDate).HasColumnName("LastLoginDate");
        this.Property(t => t.Title).HasColumnName("Title");
        this.Property(t => t.Forename).HasColumnName("Forename");
        this.Property(t => t.Surname).HasColumnName("Surname");
        this.Property(t => t.Email).HasColumnName("Email");
        this.Property(t => t.JobTitle).HasColumnName("JobTitle");
        this.Property(t => t.Telephone).HasColumnName("Telephone");
        this.Property(t => t.Mobile).HasColumnName("Mobile");
        this.Property(t => t.Photo).HasColumnName("Photo");
        this.Property(t => t.LinkedIn).HasColumnName("LinkedIn");
        this.Property(t => t.Twitter).HasColumnName("Twitter");
        this.Property(t => t.Facebook).HasColumnName("Facebook");
        this.Property(t => t.Google).HasColumnName("Google");
        this.Property(t => t.Bio).HasColumnName("Bio");
        this.Property(t => t.CompanyName).HasColumnName("CompanyName");
        this.Property(t => t.CredentialId).HasColumnName("CredentialId");
        this.Property(t => t.IsLockedOut).HasColumnName("IsLockedOut");
        this.Property(t => t.IsApproved).HasColumnName("IsApproved");
        this.Property(t => t.CanEditOwn).HasColumnName("CanEditOwn");
        this.Property(t => t.CanEdit).HasColumnName("CanEdit");
        this.Property(t => t.CanDownload).HasColumnName("CanDownload");
        this.Property(t => t.RequiresApproval).HasColumnName("RequiresApproval");
        this.Property(t => t.CanApprove).HasColumnName("CanApprove");
        this.Property(t => t.CanSync).HasColumnName("CanSync");
        this.Property(t => t.AgreedTerms).HasColumnName("AgreedTerms");
        this.Property(t => t.Deleted).HasColumnName("Deleted");
        this.Property(t => t.UserName).HasColumnName("UserName");
        this.Property(t => t.PasswordHash).HasColumnName("PasswordHash");
        this.Property(t => t.SecurityStamp).HasColumnName("SecurityStamp");
        this.Property(t => t.Discriminator).HasColumnName("Discriminator");

        // Relationships
    }
}

如您所见,声明的行

this.ToTable("Profiles");

这是导致问题的行。改变这个解决了我的问题。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2013-12-29
    • 2012-04-27
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    相关资源
    最近更新 更多