I am attempting to perform an update on only fields that have changed utilising the DbContext Entry()
method as discussed on StackOverflow here:
When I perform this on my Name
and StartDate
field everything runs perfectly, my SQL UPDATE statement includes only the Name and StartDate in the update procedure, not every field of my entity, however when I now try to update a property of a child object as well such as Company.TradingName
I receive this exception:
"The property 'Company' from the property path 'Company.TradingName' is not a complex property on type 'Franchise'. Property paths must be composed of complex properties for all except the final property."
My code simply iterates through a string array of fields (new[] {"Name", "StartDate", "Company.TradingName"}
) like this (where item
is my franchise entity my properties belong to):
foreach (var propertyName in changedPropertyNames)
{
_context.Entry(item).Property(propertyName).IsModified = true;
}
Any ideas? Updating complex properties should work according to the documentation as seen in the "Getting and setting the current or original values of complex properties" section of EF docs here.
Model as requested (generated by EF5):
public partial class Franchise
{
public System.Guid Id { get; set; }
public string Name { get; set; }
public System.DateTime StartDate { get; set; }
public virtual Company Company { get; set; }
}
The Company
model is very simple too, just public string Name, TradingName and a few others. Nothing sinister that could interfere I wouldn't think.