1

I am trying to update only the modified fields of the object in the DB Suppose the exising object in db is -

User user = new User(){
  Name = "Sid",
  Age = 20,
  Gender = "Male" 
}

and after modification it looks like this -

User user = new User(){
  Name = "Siddhant",
  Age = 20,
  Gender = "Male" 
}

Now only one field is changed that is Name, how can I run a query so that only the changes get updated in
DB.

Also, the User class in the above example could be more complex like it will have properties of user-defined data types as well.

Siddhant maurya
  • 261
  • 1
  • 2
  • 12
  • Write a [`helper method`](https://stackoverflow.com/questions/986572/how-to-quickly-check-if-two-data-transfer-objects-have-equal-properties-in-c/986617) that checks this 2 objects which properties have changed via reflection and construct the update query based on that – Kunal Mukherjee May 06 '21 at 08:26
  • Why not just update all the fields? are you worried about the overhead? – Tom Slabbaert May 06 '21 at 08:45
  • Thanks for the quick response @KunalMukherjee . I have tried exactly the same thing and it works also, but there is a limitation like what if the object becomes more complex and has deeply nested properties. So I'm not sure whether this is a correct approach or not! – Siddhant maurya May 06 '21 at 09:28
  • @TomSlabbaert Right because it may contain many properties even nested classes – Siddhant maurya May 06 '21 at 09:31

0 Answers0