I have an issue with custom users. I have a table called Profiles which I use for my users table (it wouldn't let me use Users, but that is a different issue!) and I created my dbcontext like this:
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);
}
}
As you can see, I have no had to create a DbSet for Profile because that is set in the inherited IdentityDbContext class
My profile class looks like this:
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; }
// ...
}
When I run my project I get an error stating
Invalid object name 'dbo.IdentityUsers'.
If I rename my table to IdentityUsers and run the project again I get an error stating
Invalid object name 'dbo.Profiles'.
And that is what is confusing. It looks like EF is looking for 2 tables for the Users and not just the one.
Can anyone explain to me why?
Update 1
So to try and figure this out, I removed the inherited IdentityDbContent from SkipstoneContext and just inherited from DbContext instead. The new context class looks like this:
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);
}
}
I also commented out my bindings for IdentityUserLogin, IdentityRole, IdentityUserRole and UserSecret and this is when it started working.
Here is a look at 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; }
}
It has a IdentityUser property and I think that is what is causing my issue. I know Profile inherits IdentityUser but for some reason it seems to think it is in another table.
So, my next task is to create a custom UserLogin and see if I can get EF to use that instread of IdentityUserLogin
DbSetforProfileandIdentityUser?