as I wrote in title, I have this code:
public class ApplicationUser : IdentityUser
{
public virtual MapPosition MapPosition { get; set; }
public ApplicationUser()
{
MapPosition = new MapPosition { PositionX = 0, PositionY = 0 };
}
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 class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<MapPosition> MapPositions { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
And in my controller I have method that I call from @Ajax.ActionLink in my view:
public string ChangeXPosition()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
// Get the current logged in User and look up the user in ASP.NET Identity
currentUser = manager.FindById(User.Identity.GetUserId());
currentUser.MapPosition.PositionX++;
//manager.Update(currentUser);
Debug.WriteLine("currentUser.MapPosition.PositionX: " + currentUser.MapPosition.PositionX);
return "currentUser.MapPosition.PositionX: " + currentUser.MapPosition.PositionX;
}
I want to save to database changed value of currentUser.MapPosition.PositionX. I have found numerous ways to solve this problem, but none of them worked with my project.
I tried those solutions:
how-to-update-identityuser-with-custom-properties-using-mvc5-and-entity-framewor
updating-user-data-asp-net-identity
mvc5-applicationuser-custom-properties
UPDATE Ok so I tried to do as you said, but still doesn't work:
public class ApplicationUser : IdentityUser
{
public virtual MapPosition MapPosition { get; set; }
public ApplicationUser()
{
}
}
This version works, my controller method does change the value in the table. But it works only for user, that have been already created. When I create a new User, his new MapPositions record is not created.
public class ApplicationUser : IdentityUser
{
private MapPosition _mapPosition;
public virtual MapPosition MapPosition
{
get { return _mapPosition ?? (_mapPosition = new MapPosition()); }
}
public ApplicationUser()
{
}
}
This version doesn't work at all, doesn't change the value in database and doesn't create record in MapPositions when new user is created.