Relationship between tables

I am using sqlmembership provider

and creating some tables. I need his table user

.

My application has three types of users: teachers

, students

and other users

. So I thought of three tables for them.

Each one has username

which is registered by the membership provider and stored in its own tables.

Now I don't know how to make a relationship between tables teachers

or students

with their usernames stored in the membership table using entity framework.

I thought I could add a one-to-one relationship between the table users

created by the membership provider and my tables, after reverse engineering, created tables for it for the first code, but it doesn't seem to be allowed to modify those tables.

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

More details: here are the first tables of code

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; }
}

      

+3


source to share


1 answer


I ran into a similar scenario and I ended up creating a foreign key from my user profile table to the membership table. So, for example, the Teacher table has a MembershipId column, and this will be a foreign key in 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.

+1


source







All Articles