Best way to Update or Replace Entities in Entity Framework 6
💻 coding

Best way to Update or Replace Entities in Entity Framework 6

1 min read 278 words
1 min read
ShareWhatsAppPost on X
  • 1Updating entities in Entity Framework can be tedious with manual property assignments, increasing the risk of errors.
  • 2Using DbEntityEntry's SetValues method simplifies the update process by automatically matching and updating properties.
  • 3The SetValues method can accept both entity objects and view models, enhancing flexibility in updating data.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"Updating entities in Entity Framework can be tedious with manual property assignments, increasing the risk of errors."

Best way to Update or Replace Entities in Entity Framework 6

I’ve been working with Entity Framework for a few years and one pain-point for me was updating existing data entities. It typically ended up being tedious property replacement that made the code look nasty, and caused the risk of mismatching property assignments or missing a property assignment completely. For instance:

public class MyEntityRepository
{
 public MyEntity UpdateTheOldWay(int id, MyEntity newEntityValues)
 {
 using (var context = new MyDbContext())
 {
 var entity = context.MyEntities.Find(id);
 
 entity.MyProperty = newEntityValues.MyProperty;
 entity.MyAProperty = newEntityValues.MyAProperty;
 entity.MyBProperty = newEntityValues.MyBProperty;
 entity.MyCProperty = newEntityValues.MyBProperty;
 entity.MyDProperty = newEntityValues.MyDProperty;
 entity.MyEProperty = newEntityValues.MyEProperty;

 return entity;
 }
 }
}

Spot the error? These things happen. Sometimes they get caught quick, sometimes they slip by. It also took 6 lines for this fairly small model; over time that starts to get a little nastier.

DbEntityEntry to the rescue!

I’m not sure how this alluded me for so long because it’s such a nice solution in my opinion.

public class MyEntityRepository
{
 public MyEntity Update(int id, MyEntity newEntityValues)
 {
 using (var context = new MyDbContext())
 {
 var entity = context.MyEntities.Find(id);
 context.Entry(entity).CurrentValues.SetValues(newEntityValues);
 context.SaveChanges();

 return entity;
 }
 }

 public MyEntity Update(int id, MyEntityViewModel viewModel)
 {
 using (var context = new MyDbContext())
 {
 var entity = context.MyEntities.Find(id);
 context.Entry(entity).CurrentValues.SetValues(viewModel);
 context.SaveChanges();

 return entity;
 }
 }
}

In the first method, we are replacing a MyEntity with a new MyEntity completely. Pretty cool! However, in the second method, we’re using a MyEntityViewModel. This is interesting to note; the SetValues method takes in a generic object and will update all the properties that match on both the entity and the object you pass into the SetValues method. You can also use Dictionaries with property keys as the tuple key

source: https://docs.microsoft.com/en-us/ef/ef6/saving/change-tracking/property-values

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 21 March 2019 · 1 min read · 278 words

Part of AskGif Blog · coding

You might also like