在 AspNetUsers Table 中使用 MVC 身份框架时如何在数据库中插入值?
How to insert a value in Database when using MVC identity framework in AspNetUsers Table?
我想在数据库中插入一个值,我正在使用 Asp.Net 身份框架,当新用户注册时,在我的 Register 方法中我还想添加一个新 ID,即 client_id默认情况下,身份框架不插入此 ID?
if (User.IsInRole("Avanceon"))
{
var MaxCustomerId = db.AspNetUsers.Max(x => x.Customer_Id);
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// Assign Role to user Here
await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);
// Ends Here
// await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
ViewBag.userCreated = user.UserName + " Created Successfully";
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
MaxCustomerId = MaxCustomerId + 1;
// db.AspNetUsers.LastOrDefault().Customer_Id = MaxCustomerId;
db.SaveChanges();
return View();
//return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
}
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
return View(model);
您通常不能简单地将任何数据插入 AspNetUsers
table。您需要创建一个 class 并从 IdentityUser
继承它,并在 class.
中进行必要的更改
首先你应该创建一个 class 继承 IdentityUser
的 ApplicationUser
代码。并在此处添加 属性:
public class ApplicationUser : IdentityUser
{
[Required]
public string ClientID { get; set; }
}
然后在您的代码中定位 ApplicationUser
(而不是 IdentityUser
):
var user = new ApplicationUser
{
UserName = model.UserName,
Email = model.Email,
ClientID = model.ClientID,
};
同时进行以下更改:
在ConfigureServices
方法中Startup.cs
services.AddIdentity<ApplicationUser, IdentityRole>();
在 AccountController 中,
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
添加迁移以使此更改生效
我想在数据库中插入一个值,我正在使用 Asp.Net 身份框架,当新用户注册时,在我的 Register 方法中我还想添加一个新 ID,即 client_id默认情况下,身份框架不插入此 ID?
if (User.IsInRole("Avanceon"))
{
var MaxCustomerId = db.AspNetUsers.Max(x => x.Customer_Id);
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// Assign Role to user Here
await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);
// Ends Here
// await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
ViewBag.userCreated = user.UserName + " Created Successfully";
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
MaxCustomerId = MaxCustomerId + 1;
// db.AspNetUsers.LastOrDefault().Customer_Id = MaxCustomerId;
db.SaveChanges();
return View();
//return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
}
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
return View(model);
您通常不能简单地将任何数据插入 AspNetUsers
table。您需要创建一个 class 并从 IdentityUser
继承它,并在 class.
首先你应该创建一个 class 继承 IdentityUser
的 ApplicationUser
代码。并在此处添加 属性:
public class ApplicationUser : IdentityUser
{
[Required]
public string ClientID { get; set; }
}
然后在您的代码中定位 ApplicationUser
(而不是 IdentityUser
):
var user = new ApplicationUser
{
UserName = model.UserName,
Email = model.Email,
ClientID = model.ClientID,
};
同时进行以下更改:
在ConfigureServices
方法中Startup.cs
services.AddIdentity<ApplicationUser, IdentityRole>();
在 AccountController 中,
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
添加迁移以使此更改生效