英文:
How to insert a value in Database when using MVC identity framework in AspNetUsers Table?
问题
以下是您要翻译的内容:
我想在数据库中插入一个值,我正在使用Asp.Net Identity框架,在我的注册方法中,当一个新用户注册时,我还想添加一个新的Id,这个Id默认情况下不会被Identity框架插入?
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)
{
// 在这里为用户分配角色
await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);
// 结束
// await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
ViewBag.userCreated = user.UserName + " 创建成功";
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);
英文:
I want to insert a value in DB and I am using the Asp.Net Identity framework, in my Register method when a new user registers I also want to add a new Id which is client_id by default this id is not inserted by the identity framework?
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);
答案1
得分: 2
你通常不能直接向AspNetUsers
表中插入任何数据。您需要创建一个类,继承自IdentityUser
,并在该类中进行必要的更改。
使用代码优先方法,您应该创建一个名为ApplicationUser
的类,它继承自IdentityUser
,并在其中添加属性:
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,
};
还要进行以下更改:
在Startup.cs
的ConfigureServices
方法中:
services.AddIdentity<ApplicationUser, IdentityRole>();
在AccountController
中:
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
为此更改添加迁移以生效。
英文:
You normally can't simply insert any data to AspNetUsers
table. You'll need create a class and inherit it from IdentityUser
and do the necessary changes in your class.
With code first you should create a class say ApplicationUser
which inherits IdentityUser
. And add the property there:
public class ApplicationUser : IdentityUser
{
[Required]
public string ClientID { get; set; }
}
And then target ApplicationUser
(instead of IdentityUser
) in your code:
var user = new ApplicationUser
{
UserName = model.UserName,
Email = model.Email,
ClientID = model.ClientID,
};
Make below changes as well:
In ConfigureServices
method of Startup.cs
services.AddIdentity<ApplicationUser, IdentityRole>();
In AccountController,
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
Add migration for this change to take effect
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论