Ef update single field




















FirstOrDefault ; record6. Update record6 ; dbContext. Attach record6 ; dbContext. Entry record6. Kontext Newsletter. Please log in or register to comment. Log in with external accounts Log in with Microsoft account. User posted Please give me a sample code which show how to update single field. Thursday, November 3, PM. Friday, November 4, AM. User posted Hi Mike, My approach uses a "stub" to represent the entity which does not result in a call to the database to fetch the entity.

User posted if i use second one then all fields will be updated? Friday, November 4, PM. User posted As far as I know, you could also use "SingleOrDefault" method to get the record. User posted I think your solution is better than my solution. Entry entity ; foreach var property in dbEntityEntry. Equals current dbEntityEntry. Property property. Other methods Improve this question.

Narayana Narayana 2 2 gold badges 6 6 silver badges 17 17 bronze badges. T is a domain class auto-generated by Ef. Perhaps you could use something like AutoMapper to map from your viewmodel to model? EF handles this for you. Is that your understanding?

Show 2 more comments. Active Oldest Votes. Improve this answer. Max Max 1, 1 1 gold badge 12 12 silver badges 18 18 bronze badges. If I update all the fields in a row, ALL my indexes in that table are going to get updated. Okay, most likely I wont have this logic for all entities, but in certain heavy tables, I need to have this option. That's a biiiig no no. Yeah, you're right, I'm a bit jaded about this. Not to mention potential caches invalidation and as Narayana mentions rebuilding the indices.

Just send the entire DTO back, all 45 columns and update the indexes? Notice the usage of custom convention that doesn't update Properties if they're null from server. You won't know whether the property is intentionally cleared to null OR it just didn't have any value it. In other words, the property value can only be replaced with another value but not cleared.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to update only one field using Entity Framework?

Ask Question. Asked 11 years, 4 months ago. Active 1 month ago. Viewed k times. Improve this question. Edward Brey By Password , you mean hashed password, right? Add a comment. Active Oldest Votes. Ladislav's answer updated to use DbContext introduced in EF 4. Attach user ; db. Entry user. Improve this answer. Marc Dirven 1 1 silver badge 15 15 bronze badges. Stuart Stuart 4, 2 2 gold badges 21 21 silver badges 18 18 bronze badges.

I was only able to make this code work by adding db. Which namespace to include to use db. Property "Password". This approach throws an OptimisticConcurencyException when the table has a timestamp field. I think is worth mention that if you are using db. Show 13 more comments. Attach user ; context. GetObjectStateEntry user. SetModifiedProperty "Password" ; context. Shimmy Weitzhandler 96k gold badges silver badges bronze badges. Ladislav Mrnka Ladislav Mrnka k 57 57 gold badges silver badges bronze badges.

Attach user. SaveChanges ;. Edward Brey Edward Brey You have basically two options: go the EF way all the way, in that case, you would load the object based on the userId provided - the entire object gets loaded update the password field save the object back using the context's.



0コメント

  • 1000 / 1000