Modifying ASP.NET Indentity Registration to write to two different tables

时间:2016-02-06 15:19:50

标签: c# asp.net asp.net-mvc asp.net-identity

I'm trying to customize the registration for a project using ASP.NET Identity. The registration page will have the following fields:

UserName
Email
Password
Country 
State 
Zip

The UserName, Email and Password are handled by default with the AspNetUsers table. I know I can add columns for Country, State and Zip to the AspNetUsers table. But I would like to write these fields to a different table (UserProfile) with the Id as a foreign key.

So basically when a user submits a valid registration, 3 fields will write to the default AspNetUsers table and 3 values (along with the Id) with write to a custom UserProfile table. Is there a way to do this?

1 个答案:

答案 0 :(得分:0)

您的控制器中的某个位置(很可能是'use strict'; let arr = [{ "dateTime": "2016-01-28 15:18:26", "gateType": "entry", }, { "dateTime": "2016-01-28 15:18:27", "gateType": "exit", }, { "dateTime": "2016-01-28 15:19:26", "gateType": "entry", }, { "id": 7244, "dateTime": "2016-01-28 15:19:27", "gateType": "exit", }, { "dateTime": "2016-01-28 15:20:26", "gateType": "entry", }, // here is the exit object is missing for above { "dateTime": "2016-01-28 15:21:25", "gateType": "entry", }, { "dateTime": "2016-01-28 15:21:26", "gateType": "exit", }, { "dateTime": "2016-01-28 15:22:25", "gateType": "entry", }, { "dateTime": "2016-01-28 15:22:26", "gateType": "exit", }, // here is the entry object is missing for below { "dateTime": "2016-01-28 15:23:26", "gateType": "exit", }]; function repairArr( arr ) { for(let index=0; index<arr.length; index++) { let item = arr[index]; if( index!=0 && item.gateType == arr[index-1].gateType ) { arr.splice(index, 0, { dateTime: item.dateTime, gateType: item.gateType == 'entry' ? 'exit' : 'entry' }); } }; } repairArr( arr ); console.log( arr ); ),您有一个AccountController - 方法,该方法将使用Register参数调用。

在此方法中,将使用以下内容创建新用户(其中​​RegisterViewModel的类型为model

RegisterViewModel

当新用户的创建成功(var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); result.Succeeded)时,您只需使用trueUserProfile创建新的user.Id对象, model.Countrymodel.State等,并将其存储在您的数据库中(如果您使用实体框架,则通过您的数据库上下文)。