Sean Dooley
Sean Dooley

Reputation: 625

ASP.NET MVC 3 storing additional user information in tables using Entity Framework Code First approach

I am a newbie when it comes to ASP.NET MVC so it would be great if anyone could provide guidance with the following scenario.

Loooking at a scenario whereby there will be two types of members that can register to a site, clients and suppliers.

Each type will be a member with additional information being stored in separate models/tables (i.e. Client model/table, Supplier model/table), along with default member credentials (username, email, password). Not sure of the best approach for this. Any recommendations welcome?

There will be a separate register page for each type, whereby upon successful registration they will be added to their respective role (i.e. Client, Supplier).

Here is the Client model that I'm playing with.

public class Client
{
  // Will set this manually when creating a Client member
  [DatabaseGenerated(DatabaseGeneratedOption.None)]
  public int ClientId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

Currently I've created a ViewModel as follows to handle registration for a Client

public class RegisterClient
{
  // Default ASP.NET MVC RegisterModel
  public RegisterModel RegisterModel { get; set; }
  public Client Client { get; set; }
}

Below are the Register methods in the ClientController

public ActionResult Register()
{
  return View();
}

[HttpPost]
public ActionResult Register(RegisterClient model)
{
  if (ModelState.IsValid)
  {
    // Attempt to register the user
    MembershipCreateStatus createStatus;
    Membership.CreateUser(model.RegisterModel.UserName, model.RegisterModel.Password, model.RegisterModel.Email, null, null, true, null, out createStatus);

    if (createStatus == MembershipCreateStatus.Success)
    {
      FormsAuthentication.SetAuthCookie(model.RegisterModel.UserName, false /* createPersistentCookie */);
      Roles.AddUserToRole(model.RegisterModel.UserName, "Client");
      // Get UserId for Client <-> User relationship
      int id = int.Parse(Membership.GetUser(model.RegisterModel.UserName).ProviderUserKey.ToString());
      model.Client.UserProfileId = id;
      db.Clients.Add(model.Client);
      db.SaveChanges();
      return RedirectToAction("Index", "Home");
    }
    else
    {
      ModelState.AddModelError("", createStatus.ToString());
    }
  }
  // If we got this far, something failed, redisplay form
  return View(model);
}

Is this a suitable approach? I was thinking of having the Client model inheriting MembershipUser.

How can I then create a page listing of Clients along with their membership details (i.e. username and email)?

Upvotes: 1

Views: 2058

Answers (1)

flumeware
flumeware

Reputation: 136

I have developed a system with two levels of user and the easiest way to store type specific information is to have separate tables and then to assign them a linking pieces of information, in the past I assigned all users a customer ID and then record it in the type specific tables as well. When I have wanted to look for their information I would check the users customer id then check it against the id of the record be accessed.

I your situation I would add a associate id column to both the client and supplier model. The associate ID could be the username but I have found that the best way would be to have a separate users table that links the username in asp.net membership to the associate id. Then you could easily check if the user is a client or a supplier by looking for the current users associate id in both tables. This approach means that latter if your suppliers or clients have many user accounts they can all access the same set of data. You could still use ASP.Net membership roles to perform part of the authorization ,but you should check the associate id before you show the record to the user.

UPDATE 02/05/2012:

You could create models for your user's and client's like the one's below.

public class User
{
    public int UserId{ get; set; }
    //This is from ASP.Net Membership
    public string Username { get; set; }
    public int AssosiateId { get; set; }
}

public class Client
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ClientId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    //This is the ID of the Assosiate - the same as the user's assosiate id
    public int AssosiateId { get; set; }
}

You would create a class similar to the client's one above for suppliers.

Upvotes: 1

Related Questions