How to update only one field using Entity Framework?

前端 未结 16 1861
后悔当初
后悔当初 2020-11-22 09:09

Here\'s the table

Users

UserId
UserName
Password
EmailAddress

and the code..



        
16条回答
  •  忘掉有多难
    2020-11-22 09:36

    Entity framework tracks your changes on objects that you queried from database via DbContext. For example if you DbContext instance name is dbContext

    public void ChangePassword(int userId, string password){
         var user = dbContext.Users.FirstOrDefault(u=>u.UserId == userId);
         user.password = password;
         dbContext.SaveChanges();
    }
    

提交回复
热议问题