正如我在标题中所写的,我有这样的代码:
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();
}
}
在我的控制器中,我有一个从@Ajax.ActionLink调用的方法:
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;
}
我想将currentUser.MapPosition.PositionX的更改值保存到数据库中。我已经找到了很多方法来解决这个问题,但没有一种方法适用于我的项目。
我尝试过这些解决方案:
如何更新带有自定义属性的identityuser-using-mvc5-and-entity-framewor
更新用户数据asp网络标识
mvc5应用程序用户自定义属性
更新好吧,所以我试着按照你说的做,但仍然不起作用:
public class ApplicationUser : IdentityUser
{
public virtual MapPosition MapPosition { get; set; }
public ApplicationUser()
{
}
}
这个版本有效,我的控制器方法确实更改了表中的值。但它只适用于已经创建的用户。创建新用户时,不会创建他的新MapPositions记录。
public class ApplicationUser : IdentityUser
{
private MapPosition _mapPosition;
public virtual MapPosition MapPosition
{
get { return _mapPosition ?? (_mapPosition = new MapPosition()); }
}
public ApplicationUser()
{
}
}
这个版本根本不起作用,不会更改数据库中的值,也不会在创建新用户时在MapPositions中创建记录。
至少在更新实体实例的字段/属性(在您的情况下为currentUser.MapPosition.PositionX
)后,您必须在当前ApplicationDbContext
实例上调用SaveChanges
方法:
public string ChangeXPosition()
{
var dbContext = new ApplicationDbContext();
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbContext));
// 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);
dbContext.SaveChanges();
return "currentUser.MapPosition.PositionX: " + currentUser.MapPosition.PositionX;
}
如果没有帮助,那么您必须以以下方式在DbContext的更改跟踪器中将相应的条目明确标记为Modified:
dbContext.Entry(currentUser.MapPosition).State = EntityState.Modified;
之后,应该在数据库中进行更改,您可以通过手动检查数据库中相应表中的数据(通过使用Visual Studio SQL Server data Tools扩展、SQL Server Management Studio或任何其他数据库管理工具)看到这一点。