I have landed up in a situation here.
I am loading the User from a custom function here :
public User GetUserById(int Id)
{
var User = DBContext.Users.SingleOrDefault(x=>x.Id == Id);
return User;
}
The problem is in the controller where I call this!
var User = GetUserById(Id);
User.Language = UserModel.Language;
//UpdateModel(User); //I tried this but not working.
DBContext.SaveChanges();
The database is not being updated.
I tried loading the User in the controller directly with linq and things go fine.
But, Why is it not working the other way?
Is there any workaround to make it work with this function?
Isn't there any function like DBContext.Users.Update(User). I remember using something similar, but am not able to recollect!
DBContextis a class of what?DbContextclass. It contains theDbSet<User> UsersDBContextin theGetUserByIdand theDBContextin your controller refer to the same instance? Or are they different instances? If they're different instances, then it shouldn't work because they have different change trackers.GetUserByIdas a method in theUserServicewhich is in the services namespace. and The place where I'm trying to update is UsersController.