0

I'm building an application using asp.net mvc5 with EF6. I was able to do the authentication part and even add information to the tables witch mvc5 auto-generates when we choose the Individual user accounts option. What i wanted to do now is to create a user profile. I know i can use the same table but i wanted to create another one so i can separate Authentication info from profile info. This is what i tried so far:

public class UserProfile : IdentityUser
{
    public virtual ProfileInfo Profile { get; set; }
}

public class ProfileInfo  : IdentityUser
{
    public int Id { get; set; }
    public string nome { get; set; }
    public string apelido { get; set; }
    public string idade { get; set; }
    public string sexo { get; set; }
    public string profissao { get; set; }
    public string nacionalidade { get; set; }
}

public class ProfileDbContext : IdentityDbContext<ProfileInfo>
{
    public ProfileDbContext()
        :base ("DefaultConnection" , throwIfV1Schema: false)
    {
    }
    public DbSet<ProfileInfo> Perfil { get; set; }
    public System.Data.Entity.DbSet<ProfileInfo> ProfileInfo { get; set; }
}

i couldn't find information about this anywhere else. All the tutorials i see use the AspnetUser table to store profile information.

EDIT: OK, so now i have found that what i need is just a foreignKey to the table Asp.netUsers. Problem is, the primary key from that table is a varChar... not an int. Damn Microsoft...

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Pedro Costa
  • 427
  • 3
  • 13
  • 31
  • 1
    If you want a separate table why not use a foriegn key to `IdentityUser.Id` and the related virtual property instead of inheritance? – Jasen Jan 08 '15 at 18:30
  • well thats my problem, i dont know how to do that and everything i see on the web explains how to do it using the same table :S – Pedro Costa Jan 08 '15 at 19:19
  • 1
    Sounds like you just need to configure a one-to-one relationship between an `IdentityUser` and a `ProfileInfo`. Try this [answer](http://stackoverflow.com/a/10837964/2030565). – Jasen Jan 08 '15 at 19:55
  • problem is, how do u define the foreign keys to asp.netUsers ?? – Pedro Costa Jan 11 '15 at 15:03
  • You can still use varchar `IdentityUser.Id` as a FK on `ProfileInfo`. Just make `ProfileInfo.IdentityUserId` varchar (string) as well. Alternatively, but it's more work, you can change Identity to use int instead of the default string type -- there are other SO posts that cover that. – Jasen Jan 11 '15 at 20:47
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 11 '15 at 20:55

1 Answers1

1

As mentioned in the comments, defined a relationship from UserProfile to ProfileInfo.

public class UserProfile : IdentityUser
{
    [ForeignKey("ProfileInfo")]
    public int ProfileInfoId Profile { get; set; }
    public ProfileInfo ProfileInfo { get;set; }
}
Brendan Green
  • 11,676
  • 5
  • 44
  • 76