Answered by:
LightSwitch - SaveChanges/GetChanges ignoring Foreign Key changes

Question
-
I'm basically following the Audit Trail solution by Paul Van Bladel: http://blog.pragmaswitch.com/?p=291. I ran into an issue where the returned changes don't include entries for the foreign key fields.
Here's the SaveChanges that calls CreateModifyAuditRecord which works fine:
partial void SaveChanges_Executing() { EntityChangeSet changes = this.DataWorkspace.TrackerData.Details.GetChanges(); var x = this.DataWorkspace.TrackerData.Details.Properties.Incidents; foreach (var mod in changes.ModifiedEntities) { CreateModifyAuditRecord(mod); } }
Here's where the problem occurs:
private void CreateModifyAuditRecord(IEntityObject entity) { var changes = entity.Details.Properties.All().OfType<IEntityStorageProperty>(); foreach (IEntityStorageProperty chg in changes) { if (chg.Name != "RowVersion") if (!object.Equals(chg.Value, chg.OriginalValue)) { AuditIncident change = this.DataWorkspace.TrackerData.AuditIncidents.AddNew(); change.ChangedBy = this.Application.User.FullName; change.ChangedDate = DateTime.Now; change.ChangeType = "Updated"; change.ChangedProperty = chg.Name; change.OldValue = chg.OriginalValue.ToString(); change.NewValue = chg.Value.ToString(); change.EntitytId = (int)entity.Details.Properties["Id"].Value; } } }
The "foreach (IEntityStorageProperty chg in changes)" includes the RowVersion change when a child table is updated, but the property that changed (LookupProvider) is not listed. This is a multi-select relationship to tie multiple Providers to an Incident.
Any ideas on how to capture this info since there's no Property/Value/OldValue in the collection?
Thank you.
aRBee
Tuesday, May 28, 2013 9:09 PM
Answers
-
Change ientitystorageproperty to ientitytrackedproperty and it should work.
- Marked as answer by aRBee - Rob Brown Wednesday, May 29, 2013 1:28 PM
Wednesday, May 29, 2013 3:53 AM
All replies
-
Hi. I'm not at my dev machine but if I recall, I think you need to also loop through the navigation properties. Check Intelisense for for something like "IEntityNavigationProperty".
Hope this helps.
Brian
Wednesday, May 29, 2013 1:58 AM -
Change ientitystorageproperty to ientitytrackedproperty and it should work.
- Marked as answer by aRBee - Rob Brown Wednesday, May 29, 2013 1:28 PM
Wednesday, May 29, 2013 3:53 AM -
That did it! Thanks!!Wednesday, May 29, 2013 1:29 PM
-
Great, glad it works!Thursday, May 30, 2013 1:51 AM
-
Hi,
I have also added relationship tables. still i am not able to track those properties using ientitytrackedproperty . any help on this?
Thanks in Advance.
Wednesday, June 24, 2015 10:05 AM