首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注册时如何将配置文件表链接到AspNetUsers表?

注册时如何将配置文件表链接到AspNetUsers表?
EN

Stack Overflow用户
提问于 2016-12-10 18:52:15
回答 1查看 569关注 0票数 0

所以我正在用MVC制作一个.NET核心应用程序,我想知道如何做一个新的配置文件,同时在我的AspNetUsers中把该行的id作为"ProfileId“来注册。

这是我的Profile.cs:

代码语言:javascript
复制
namespace Overnight.Models
{
      public enum GenderType : byte {
        Unknown = 0,
        Male = 1,
        Female = 2,
        NotApplicable = 9
    }
    public class Profile : BaseEntity<Int64>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; } 
        public GenderType Gender { get; set; }
        public Nullable<DateTime> DayOfBirth { get; set; } 
        public Nullable<DateTime> LastActivityDate { get; set; }
        //TODO One to One reference for image and adress 
        public List<ProfileReview> Reviews { get; set; }
        public List<Blog> Blogs { get; set; } 
         public List<Accomodation> Accomodations { get; set; } 
        public List<Post> Posts { get; set; }
         public List<Wishlist> Wishlists { get; set; }
        public List<Invoice> Invoices { get; set; }
        public List<Discount> Discounts { get; set; }

        public Security.ApplicationUser ApplicationUser { get; set; }

    }
}

这是我的ApplicationUser.cs:

代码语言:javascript
复制
namespace Overnight.Models.Security
{
     public class ApplicationUser : IdentityUser<Guid> 
     {
         public string PlainPassword  { get; set; }

         public DateTime CreatedAt {get; set;}

        public Nullable<DateTime> UpdatedAt {get; set;}

        public  Nullable<DateTime> DeletedAt {get; set;}

        public Int64 ProfileId { get; set; }

        public Profile Profile { get; set; }
    }
}

下面是在我的AccountController.cs中注册的代码:

代码语言:javascript
复制
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {

                var user = new ApplicationUser { UserName = model.Email, Email = model.Email};    
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {

                    await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    return RedirectToLocal(returnUrl);
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-10 19:49:28

所以不确定这是否是最好的方法,但我认为您应该首先创建一个profile,然后将它插入到AspNetUser表中,您还必须想出一个‘回滚’机制,要么使用存储过程,要么手动跟踪您的工作,如果有什么问题就回溯。

因此,让您的RegisterViewModel包含创建Profile所需的信息,在创建user之前,应该有如下一行:

代码语言:javascript
复制
var profile = profileRepo.add(new profile { /* use registerViewModel to fill*/});
profleRepo.SaveChanges(); // now your profile has the Id of last inserted row

然后你就可以这样做:

代码语言:javascript
复制
 var user = new ApplicationUser { UserName = model.Email, Email = model.Email, profileId = profile.Id};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41079099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档