更改Identity User的自定义字段

时间:2017-06-05 14:59:16

标签: c# asp.net-web-api asp.net-identity

嘿,我将Teams添加到我的ApplicationUser中:

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

在我的控制器中,我想为用户添加一个新的团队:

ApplicationUser user = UserManager.GetUserById(dto.PersonId[0]);
if (user != null)
{
    if (user.Teams == null)
    {
        user.Teams = new List<Team>();
    }
    user.Teams.Add(mannschaft);
    mannschaft.UsersId.Add(user?.Id);
    uow.SaveChanges();
    Team team = uow.RepTeam.Get(t => t.Bezeichnung == dto.Name).FirstOrDefault();
    return Request.CreateResponse(HttpStatusCode.Created, team?.Id);
}

所以团队加入IdentityUser没有问题,但是当我想在不同的Controller中调用所有团队时,这样:

ApplicationUser user = UserManager.GetUserById(userid);
ICollection<DtoTeam> foo = new List<DtoTeam>();
if (user != null)
{
    foreach (var teams in user.Teams)
    {
        foo.Add(uow.RepTeam.GetByKey(teams).ToDto());
    }
}
return foo;

它说用户中没有团队......

1 个答案:

答案 0 :(得分:0)

IMO,您仍然可以使用您的表并删除自动生成表,如Aspnet .Table或其他

如果您想保存数据,可以使用OnmodelCreating()

将identityUser之类的路径更改为您的表
protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().ToTable("your_table_name");           
    }

您可以搜索谷歌的任何进展