记开发个人图书收藏清单小程序开发(八)初始化书房信息修改

因为之前逻辑设计有点小问题,所以修改了一下:

增加core._User的Table:

1 CREATE TABLE [core].[_User]
2 (
3     [ID]       INT         NOT NULL PRIMARY KEY, 
4     [StringID] VARCHAR(36) NOT NULL,
5     CONSTRAINT [FK_User_Party] FOREIGN KEY ([ID]) REFERENCES [core].[_Party] ([ID])
6 )

在Web端需要传入StringID(也即微软Identity自动生成的AspNetUsers Table的ID)。

WebUI的Project增加Service:

/Services/UserManagerService.cs

 1     public class UserManagerService : UserManager<IdentityUser>
 2     {
 3         public UserManagerService(IUserStore<IdentityUser> store
 4             , IOptions<IdentityOptions> optionsAccessor
 5             , IPasswordHasher<IdentityUser> passwordHasher
 6             , IEnumerable<IUserValidator<IdentityUser>> userValidators
 7             , IEnumerable<IPasswordValidator<IdentityUser>> passwordValidators
 8             , ILookupNormalizer keyNormalizer
 9             , IdentityErrorDescriber errors
10             , IServiceProvider services
11             , ILogger<UserManager<IdentityUser>> logger)
12             : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
13         {
14         }
15         
16     }

当然,也需要在Startup.cs文件的ConfigureServices中增加依赖注入:

1 services.AddScoped<UserManagerService>();

修改Init.cshtml.cs中的部分:

 1     using M = Shelf;
 2     public class InitModel : PageModel
 3     {
 4         private readonly IShelfRepo _shelfRepo;
 5         private readonly UserManagerService _userManagerService;
 6         public InitModel(IShelfRepo shelfRepo
 7             , UserManagerService userManagerService)
 8         {
 9             _shelfRepo = shelfRepo;
10             _userManagerService = userManagerService;
11         }
12         [BindProperty]
13         public InitInputModel Input { get; set; }
14 
15         public void OnGet()
16         {
17 
18         }
19 
20         public async Task<IActionResult> OnPostAsync()
21         {
22             if (ModelState.IsValid)
23             {
24                 await _shelfRepo.InitAsync(new M.InitSpec
25                 {
26                     StringID = _userManagerService.GetUserId(User),
27                     NickName = Input.NickName.Trim(),
28                     ShelfName = Input.ShelfName.Trim()
29                 });
30                 return RedirectToPage("New");
31             }
32             return Page();
33         }
34     }

主要是第26行。对应的给InitSpec Model也增加StringID的字段。

好了,现在BL DB和登录信息关联上了。

 

posted @ 2018-06-25 18:58  Jnetart  阅读(182)  评论(0编辑  收藏  举报