0

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?

enter image description here

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?

enter image description here

Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78
  • AspNetUsers is the user table for identity. The other tables are also prefixed with AspNet by default (AspNetUserRoles, AspNetUserLogins, etc). Did you upgrade from Membership or something? – Steve Greene Jul 23 '15 at 17:01
  • Im pretty new to this identityUsers thing. Can you explain some more? What do you mean by updated membership? – Daniel Gustafsson Jul 23 '15 at 17:03
  • Just trying to understand why your table names are not standard. Is this a fresh app? http://www.typecastexception.com/post/2014/04/20/ASPNET-MVC-and-Identity-20-Understanding-the-Basics.aspx – Steve Greene Jul 23 '15 at 17:09
  • Ah i changed them using modelbuilder. see my edit! – Daniel Gustafsson Jul 23 '15 at 17:09
  • Your issue might be related to having separate contexts for identity versus your stuff. You could inherit from IdentityDbContext and have it all under a single context. Otherwise you can add the DbSet's for the identity stuff to your context. Still odd that you have a Users table and an AspNetUsers table that are essentially the same thing. Finally, make sure you run base.OnModelCreating(modelBuilder); before your other modelbuilder stuff. http://stackoverflow.com/questions/22855428/how-to-change-table-names-for-asp-net-identity-2-0-with-int-id-columns – Steve Greene Jul 23 '15 at 17:20
  • I changed to so that they are in only one context and i added many to many relation between applicationuser and my entity blog but is doesn't make the relation correct. See my edit – Daniel Gustafsson Jul 23 '15 at 17:26
  • 1
    You shouldn't need the DbSet for ApplicationUser - that is provided automatically. Many to many can be tricky. You might want to configure the bridge table BlogUser manually. https://msdn.microsoft.com/en-us/data/jj591620.aspx#ManyToMany – Steve Greene Jul 23 '15 at 17:35

0 Answers0