Last post Apr 10, 2017 08:47 AM by Cathy Zou
Apr 09, 2017 04:40 PM|Tonka742|LINK
Hi, i have spent a good 9 hours trying to figure out how to do this. I need to save a new piece of data to my table where user data is stored by default in ASP. I have added the required columns but i just cannot find the code that i need to use in order
to change values in the table.
For example if my users were saved and i have accounts set up. i need to be able to add another piece of data to the logged in users table data. so if the user was setup without an age but wanted to add one, they could simply enter it into my form and it
would save into the database under the column of age fore their particular login id.
I need to take a variable and save it into the users table.
Can anyone show me the code or direct me to somewhere where i can work it out because right now i'm stumped. Please!
Apr 09, 2017 06:23 PM|raju dasa|LINK
Identity uses EF with its own DbContext,
use this dbcontext to update user data,
but not possible to directly update from Identity.
Apr 09, 2017 06:25 PM|Tonka742|LINK
so how can i do it then? There is add password code in the default asp template with individual user accounts but i cant work out how to change that
Apr 10, 2017 08:47 AM|Cathy Zou|LINK
Form your description. are you wan to update a user record?
If that the case. I suggest you could use UserManager like below:
using (ApplicationDbContext dbCtx = new ApplicationDbContext())
// use the same context for the UserManager
UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbCtx));
// user to update
var user = UserManager.Users
.First(u => u.Id == User.Identity.GetUserId());
// movie to update
var movie = dbCtx.Movies.SingleOrDefault(m => m.Name == "Star Wars");
// this is the only property i want to update
movie.IsRented = true;
// user update
user.IsRenting = true;
user.MovieRented = movie;
// this is should do the trick