From the regular aspnet_Users table I have created two sub-tables with one to one relationship on UserId.
Table UserClient
-UserId int PK
-ClientNumber int
Table UserEmployee
-UserId int PK
-EmployeeNumber int
I have created a Table per Type model in the edmx and now wish to create the appropriate record in the appropriate sub-table when I create a new aspnet_User record through the membership stored procedures, as shown below:
[HttpPost]
public ActionResult CreateUser(CreateUserModel model)
{
if (ModelState.IsValid)
{
// Attempt to create the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == MembershipCreateStatus.Success)
{
//now that the aspnet_user record is created...
FilingBizEntities db = new FilingBizEntities();
//Get the role chosen for the user from selectlist on the CreateUser page
var thisrole = db.aspnet_Role.Where(rn => rn.RoleId == model.RoleId).FirstOrDefault();
//Add the user to the role
Roles.AddUserToRole(model.UserName, thisrole.RoleName);
return RedirectToAction("Index", "Users");
}
else
{
ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
}
}
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View(model);
}
Whether it is of type "UserClient" or "UserEmployee" is dependent on its RoleId, which I currently differentiate when needed by query. Now I wish to extend each type's properties by having tables.
Let's say the newly created user is a Client.
How do I insert a record into the UserClient table with it's aspnet_Users' UserId and this ClientNumber?
The best way to create a Client for an existing User is to use a stored procedure; not a stored procedure that is wired up to the Client entity through mappings, but a separate one that can be called explicitly from the code.
Ideally it would be a SP that takes a UserId as a parameter, inserts a new row into the UserClient table using that UserId, and then returns a complete Client object so that it can be used immidiately.