Blazi
Blazi

Reputation: 1011

Relationship between tables

I use sqlmembership provider and that creates some tables. I need its user table.

My application has three types of users: teachers, students and other users. So I thought of three tables for them.

Each of them has a username which is registered by membership provider and saved in its own tables.

Now I don't know how to make the relationship between teachers or students tables to their usernames saved in the table of memberships, using entity framework.

I thought I can add a one to one relationship between users table created by membership provider and my tables after reverse engineering created tables by it to code first, but it seems is not allowed to change those tables.

Can someone show me how to make a relationship between teachers or students to their usernames (I mean their membership information)?

More Details: here are code first tables

public class aspnet_Users
{
    public aspnet_Users()
    {
        this.aspnet_PersonalizationPerUser = new List<aspnet_PersonalizationPerUser>();
        this.aspnet_Roles = new List<aspnet_Roles>();
    }

    public System.Guid ApplicationId { get; set; }
    public System.Guid UserId { get; set; }
    public string UserName { get; set; }
    public string LoweredUserName { get; set; }
    public string MobileAlias { get; set; }
    public bool IsAnonymous { get; set; }
    public System.DateTime LastActivityDate { get; set; }
    public virtual aspnet_Applications aspnet_Applications { get; set; }
    public virtual aspnet_Membership aspnet_Membership { get; set; }
    public virtual ICollection<aspnet_PersonalizationPerUser> aspnet_PersonalizationPerUser { get; set; }
    public virtual aspnet_Profile aspnet_Profile { get; set; }
    public virtual ICollection<aspnet_Roles> aspnet_Roles { get; set; }
}

public class Techer
{
    [Key]
    public int TeacherId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string NationalNumber { get; set; }
    public string PhoneNumber { get; set; }
    public string Description { get; set; }
    public int OfficeId { get; set; }
    public virtual Office Office { get; set; }
}

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int OfficeId { get; set; }
    public virtual Office Office { get; set; }
}

Upvotes: 3

Views: 365

Answers (1)

Will
Will

Reputation: 895

I've encountered a similar scenario, and what I ended up doing is creating a foreign key from my user profile table(s) to the membership table. So for example, the Teacher table would have a column called MembershipId, and it would foreign key to the Id column in the membership table. Then, in the Register action of my AccountController, I have something like this:

if (db.Memberships.Any(x => x.UserName == model.UserName) 
{
    // handle error here
    // return view with error message "user name already in use"
}

var token = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, true);
var membership = db.Memberships.SingleOrDefault(x => x.UserName == model.UserName);
if (membership != null) 
{
    var newProfile = new Teacher
    {
        Membership = membership
        // add other properties here if required
    }
    db.Teachers.Add(newProfile);
    db.SaveChanges();
}

*This code is untested, as I don't have VS available at the moment, but should be enough to get you on the right track.

Upvotes: 1

Related Questions