Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Identity 2保存扩展应用程序用户时,会插入重复数据或保存两次_C#_Asp.net_Webforms_Asp.net Identity 2 - Fatal编程技术网

C# ASP.NET Identity 2保存扩展应用程序用户时,会插入重复数据或保存两次

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

我向ApplicationUser添加了一些字段,如下所示:

 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));