ASP.net Core Entity Framework CurrentValues.SetValues() best current alternative

asp.net asp.net-core entity-framework entity-framework-core

Question

As those of you working with Entity Framework Core may know, the current version does not have an implementation for CurrentValues.SetValues() for Entities. It looks like support for this will come in the next release v1.1 as this issue states. Until then, does anybody have a solution that works for updating entity entries?

For context, the SetValues function in the past entity framework takes an entity and updates it properties to the values of an entity passed as a parameter i.e.:

var updatedEntity = currentEntity.CurrentValues.SetValues(newValues);
_dbContext.SaveChanges();

I've found some implementations using PropertyInfo to make a generic update function but haven't found anything that looks to be a nice clean solution. Might just have to take what I can get though :)

p.s. I realize this function only worked for simple entities that don't have complex object graphs that would require updating other related entities. My use case is simple. It's just annoying to have to manually map properties right now.

1
3
11/14/2016 8:20:03 PM

Accepted Answer

EDIT: Now that EF Core v1.1.0 has been released, CurrentValues.SetValues() will work.

See the following code snippet

var currentEntity = dbSetEntity.Find(id);
_dbContext.Entry(currentEntity).CurrentValues.SetValues(newValues);
4
1/17/2017 6:15:08 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow