Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First Modeling with Membership Tables

I want to track additional user profile information in my own table rather than the default tables setup by ASP.Net. So how do you make a foreign key that maps to the aspnet_Users.UserId field in my UserInfoModel class?

public class UserInfoModel
{

    [MaxLength(30)]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [MaxLength(50)]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
}
like image 418
ChiliYago Avatar asked Mar 31 '26 18:03

ChiliYago


1 Answers

I've done something similar:

public class User
{
    [Key]
    public Guid UserId { get; set; }
    public string UserName { get; set; }
}

public class UserDetail
{
    [Key]
    public Guid UserId { get; set; }
    public string FullName { get; set; }
    public string CompanyName { get; set; }
}

public class UserMembership
{
    [Key]
    public Guid UserId { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public bool IsApproved { get; set; }
    public bool IsLockedOut { get; set; }
}

And here are the configurations for those tables:

public class UserConfig : EntityConfiguration<User>
{
    public UserConfig()
    {
        this.MapSingleType(user => new
        {
            UserId = user.UserId,
            UserName = user.UserName
        }).ToTable(new StoreTableName("aspnet_Users", "dbo"));
    }
}

public class UserDetailConfig : EntityConfiguration<UserDetail>
{
    public UserDetailConfig()
    {
        this.HasKey(u => u.UserId);
        this.MapSingleType(ud => new
        {
            UserId = ud.UserId,
            FullName = ud.FullName,
            CompanyName = ud.CompanyName
        }).ToTable(new StoreTableName("UserDetail", "dbo"));
    }
}

public class UserMembershipConfig : EntityConfiguration<UserMembership>
{
    public UserMembershipConfig()
    {
        this.HasKey(m => m.UserId);

        this.MapSingleType(membership => new
        {
            UserId = membership.UserId,
            Password = membership.Password,
            Email = membership.Email,
            IsApproved = membership.IsApproved,
            IsLockedOut = membership.IsLockedOut
        }).ToTable(new StoreTableName("aspnet_Membership", "dbo"));
    }
}

Your database context:

public class TLI_FBA : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<UserDetail> UserDetails { get; set; }
    public DbSet<UserMembership> UserMemberships { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfig());
        modelBuilder.Configurations.Add(new UserDetailConfig());
        modelBuilder.Configurations.Add(new UserMembershipConfig());
    }
}
like image 157
Saxman Avatar answered Apr 02 '26 09:04

Saxman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!