I'm working on an EF 5 Code-First solution and I am trying to update an existing entity with a modified one using the Repository pattern:
public void UpdateValues(T originalEntity, T modifiedEntity)
{
_uow.Context.Entry(originalEntity).CurrentValues.SetValues(modifiedEntity);
My simplified domain object looks like this:
public class PolicyInformation : DomainObject
{
//Non-navigation properties
public string Description {get;set;}
public bool Reinsurance {get;set;}
...
//Navigation properties
LookupListItemFormType FormType {get;set;}
...
}
The problem I have is that the CurrentValues.SetValues(modifiedEntity);
method seems to be updating only scalar and complex type properties but not Navigation properties.
I've seen this happening to a lot of people and still don't know why that is. However, I figured out that if I set those navigation properties manually after executing my UpdateValues
everything works fine:
_policyInfoRepo.UpdateValues(existingPolicyInfo, info);
existingPolicyInfo.FormType = info.FormType;
The question is, since I use a generic repository: How can I get a list of Navigation properties in my domain object? Perhaps through linq, reflection, or any other way so my update method in my repository can loop through them and set them automatically?
Something like this:
public void UpdateValues(T originalEntity, T modifiedEntity)
{
//Set non-nav props
_uow.Context.Entry(originalEntity).CurrentValues.SetValues(modifiedEntity);
//Set nav props
var navProps = GetNavigationProperties(originalEntity);
foreach(var navProp in navProps)
{
//Set originalEntity prop value to modifiedEntity value
}
Thanks!
CrowdWorxEntities
is? – Mazza