Hello,
I wish to update a particular field in DB by passing the field name and value. How can I make below work ?
await _context.
Hello,
I wish to update a particular field in DB by passing the field name and value. How can I make below work ?
await _context.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mayooran NavamanyPosted Jun 20, 2024, 3:17 AM
Here's how you can achieve this using reflection:
Below is an example of how you can implement this:
Here’s how you can call this method:
Explanation
Retrieve the Entity:
This line retrieves the entity from the database using its ID.
Use Reflection to Set the Property:
This code uses reflection to find the property by name and set its value. The
Convert.ChangeTypemethod is used to ensure the value is of the correct typeSave Changes:
This line saves the changes back to the database.
Note