所以我正在用MVC制作一个.NET核心应用程序,我想知道如何做一个新的配置文件,同时在我的AspNetUsers中把该行的id作为"ProfileId“来注册。
这是我的Profile.cs:
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:
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中注册的代码:
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);
}发布于 2016-12-10 19:49:28
所以不确定这是否是最好的方法,但我认为您应该首先创建一个profile,然后将它插入到AspNetUser表中,您还必须想出一个‘回滚’机制,要么使用存储过程,要么手动跟踪您的工作,如果有什么问题就回溯。
因此,让您的RegisterViewModel包含创建Profile所需的信息,在创建user之前,应该有如下一行:
var profile = profileRepo.add(new profile { /* use registerViewModel to fill*/});
profleRepo.SaveChanges(); // now your profile has the Id of last inserted row然后你就可以这样做:
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, profileId = profile.Id};https://stackoverflow.com/questions/41079099
复制相似问题