Ok, i want to add properties to the user table created by asp identity.
So i read about it and found out that i can do it by doing this:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
public IList<Blog> Blogs { get; set; }
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
return userIdentity;
}
}
So the properties was added to the table dbo.AspNetUsers and not to the users table. Why?
So i thought maybe it just places the properties there but i can use them in the same dbset so i added the this code:
public class TravelContext : DbContext
{
static TravelContext()
{
System.Data.Entity.Database.SetInitializer<TravelContext>(new CreateDatabaseIfNotExists<TravelContext>());
}
public TravelContext() : base("Name=TravelDB2")
{
}
public DbSet<Blog> Blog { get; set; }
public DbSet<Continent> Continent { get; set; }
public DbSet<Country> Country { get; set; }
public DbSet<Destination> Destinations { get; set; }
public DbSet<Travel> Travel { get; set; }
public DbSet<IdentityUser> User { get; set; }
}
but that doesn't help me. I still can't for example firstname from my user i can only reach the properties from the Users table.
So how do i get it to add the properties to the right table and how do i add them in the context so that i can use them using EF? EDIT:
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin", "dbo");
EIDT NEW CONTEXT:
public class TravelContext : IdentityDbContext<ApplicationUser>
{
static TravelContext()
{
System.Data.Entity.Database.SetInitializer<TravelContext>(new CreateDatabaseIfNotExists<TravelContext>());
}
public static TravelContext Create()
{
return new TravelContext();
}
public TravelContext()
: base("Name=TravelDB2")
{
}
public DbSet<Blog> Blog { get; set; }
public DbSet<Continent> Continent { get; set; }
public DbSet<Country> Country { get; set; }
public DbSet<Destination> Destinations { get; set; }
public DbSet<Travel> Travel { get; set; }
public DbSet<IdentityUser> User { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin", "dbo");
modelBuilder.Entity<Blog>().HasMany(a => a.Users);
modelBuilder.Entity<ApplicationUser>().HasMany(a => a.Blogs);
}
}
so now it adds the id of blog to my user table and the id of the user to my blog entity but i added HasMany so it should create an many-to-many relation?