0

How could I update only one field of my table by EF core? In another language how could I translate below SQL statement into EF Core :

UPDATE table_name SET column1 = value1 WHERE condition;

Abdulaziz
  • 654
  • 3
  • 12
  • 37
  • Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Please show your attempts you have tried and the problem/error messages you get from your attempts. – Progman Aug 28 '20 at 17:24

2 Answers2

0

I update it by below code

 public async Task updateUserInfo(string token,long  userId)
        { 
User user = _userRepository.GetDbContext().Set<User>().Find(userId);

            user.token= token;

            _userRepository.GetDbContext().Set<User>().Attach(user);

            _userRepository.GetDbContext().Entry(user).Property(x => x.token).IsModified =true;
           await _userRepository.GetDbContext().SaveChangesAsync();
}
Abdulaziz
  • 654
  • 3
  • 12
  • 37
-1

I searched it on google and found a small example about this.

 var stud = new Student(){ StudentId = 1, Name = "Bill" };
    
    stud.Name = "Steve"; 
    
    using (var context = new SchoolContext())
    {
        context.Update<Student>(stud);
    
        context.SaveChanges(); 
    }

I hope it will help your problem.

  • Do not provide link only answers, see https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Progman Aug 28 '20 at 17:52
  • Do not remove the link entirely, though. This is someone else's code that you're pasting, so you need to acknowledge that in your answer. – David Buck Aug 29 '20 at 07:19