C# 从标识用户更新自定义字段
嘿,我尝试保存身份用户的更新字段,如下所示:C# 从标识用户更新自定义字段,c#,asp.net-mvc,asp.net-web-api,asp.net-identity,C#,Asp.net Mvc,Asp.net Web Api,Asp.net Identity,嘿,我尝试保存身份用户的更新字段,如下所示: var storage = new UserStore<ApplicationUser>(new SpomunicateContext()); var manager = new Microsoft.AspNet.Identity.UserManager<ApplicationUser>(storage); await manager.UpdateAsync(user); var storage=newuserstor
var storage = new UserStore<ApplicationUser>(new SpomunicateContext());
var manager = new Microsoft.AspNet.Identity.UserManager<ApplicationUser>(storage);
await manager.UpdateAsync(user);
var storage=newuserstore(new SpomunicateContext());
var manager=new Microsoft.AspNet.Identity.UserManager(存储);
wait manager.UpdateAsync(用户);
这不起作用,因为我遇到以下错误:
“附加'SpommunicateWebAPI.Models.ApplicationUser'类型的实体失败,因为同一类型的另一个实体已具有相同的主键值。”。如果图形中的任何实体具有冲突的键值,则在使用“Attach”方法或将实体状态设置为“Unchanged”或“Modified”时可能会发生这种情况。这可能是因为某些实体是新的,尚未收到数据库生成的键值。在这种情况下,使用“添加”方法或“添加”实体状态跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改”
我不知道发生了什么..没有像应用程序用户这样的实体。
我只上了这门课:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public virtual ICollection<Function> Functions { get; set; }
public virtual ICollection<Participation> Participations { get; set; }
public virtual ICollection<Team> Teams { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Mobile { get; set; }
public DateTime Birthdate { get; set; }
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
公共虚拟ICollection函数{get;set;}
公共虚拟ICollection参与{get;set;}
公共虚拟ICollection团队{get;set;}
公共字符串名{get;set;}
公共字符串Lastname{get;set;}
公共字符串Mobile{get;set;}
公共日期时间出生日期{get;set;}
}
}
这可能是问题所在吗?
在owin之前,我得到了一个具有相同字段的实体人,但我删除了我的Db并删除了实体,所以我认为这不可能是问题所在
编辑
我从这里获取我的用户:
public static ApplicationUser GetUserByUsername(string username)
{
return HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindByNameAsync(username).Result;
}
public静态应用程序用户GetUserByUsername(字符串用户名)
{
返回HttpContext.Current.GetOwinContext().GetUserManager().FindByNameAsync(username.Result);
}
ApplicationUser类是您的实体,它继承自IdentityUser。这两个类都默认映射到AspNetUsers表。我看到您有一个用户要更新,但您能否显示从何处获取用户?将其添加到问题中。。