從常規的aspnet_Users表我創建了兩個子表,在UserId上具有一對一的關係。如何應用Table-per-type EF4在從aspnet_Users繼承的子表中創建記錄?
Table UserClient
-UserId int PK
-ClientNumber int
Table UserEmployee
-UserId int PK
-EmployeeNumber int
我在EDMX創建每種類型模型中的表,現在希望在適當的子表中創建相應的記錄,當我創建通過會員存儲程序的新aspnet_User記錄,如下圖所示:
[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);
}
無論是類型「UserClient」或「UserEmployee」的依賴於它的角色ID,在需要時通過查詢我目前區分。現在我希望通過表擴展每個類型的屬性。
假設新創建的用戶是客戶端。
如何使用aspnet_Users的UserId和此ClientNumber將記錄插入到UserClient表中?
謝謝,我已經成功地使用了這種方法。 – 2010-11-24 04:05:54
沒問題,很高興它幫助:) – 2010-11-24 04:07:18