C# ASP.NET Identity 2保存扩展应用程序用户时,会插入重复数据或保存两次
我向ApplicationUser添加了一些字段,如下所示:C# ASP.NET Identity 2保存扩展应用程序用户时,会插入重复数据或保存两次,c#,asp.net,webforms,asp.net-identity-2,C#,Asp.net,Webforms,Asp.net Identity 2,我向ApplicationUser添加了一些字段,如下所示: public class ApplicationUser : IdentityUser { public virtual UserType Type { get; set; } public string FirstName { get; set; } public string CompanyName { get; set; } } 使用此代码注册用户时,它会自动插入重复的数据类型(在UserTyp
public class ApplicationUser : IdentityUser
{
public virtual UserType Type { get; set; }
public string FirstName { get; set; }
public string CompanyName { get; set; }
}
使用此代码注册用户时,它会自动插入重复的数据类型(在UserType
表中)。请看以下代码:
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
Guid typeID = Guid.Parse(drpType.SelectedValue);
var type = db.UserTypes.First(t => t.ID == typeID);
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text, FirstName = FirstName.Text, CompanyName = CompanyName.Text, Contacts = Contacts.Text, Type = type };
type=null;
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
...
}
}
protectedvoid CreateUser\u单击(对象发送方,事件参数e)
{
var manager=Context.GetOwinContext().GetUserManager();
var signInManager=Context.GetOwinContext().Get();
Guid typeID=Guid.Parse(drpType.SelectedValue);
var type=db.UserTypes.First(t=>t.ID==typeID);
var user=new ApplicationUser(){UserName=Email.Text,Email=Email.Text,FirstName=FirstName.Text,CompanyName=CompanyName.Text,Contacts=Contacts.Text,Type=Type};
type=null;
IdentityResult result=manager.Create(user、Password.Text);
if(result.successed)
{
...
}
}
请帮助我解决这个问题,当我调用manager.Create()时会发生什么情况?为什么它会将类型插入数据库?您应该在GetOwinContext的帮助下获取类型对象。以下代码应该可以解决您的问题:
var dbcontext= Context.GetOwinContext().Get<ApplicaitionDbContext>();
var type = dbcontext.UserTypes.First(t => t.ID.Equals(typeID));
var dbcontext=Context.GetOwinContext().Get();
var type=dbcontext.UserTypes.First(t=>t.ID.Equals(typeID));