Records in ChangeSet.Deletes have nulled relations
Posted: Mon 30 Sep 2013 08:14
Hello,
I am writing a custom logger implementation that traverses the set of pending changes and composes log messages, which are in turn specific to change action (insert/delete/update) and object type. For certain object types I need to obtain and log specific data from relations. E.g. I have record types Foo and Bar - the type Foo has field bar of type Bar, which is essentialy an N-to-1 relation. Suppose I have the code below:
The problem with this code is that all the records of type Foo, which are marked for deletion, have their bar fields set to null (as well as barid foreign keys set to 0). Thus, such an attempt to obtain the name of the Bar associated with a Foo being deleted results in a NullReferenceException.
Is there a way to obtain the Bar name that would work in this case? All data manipulations are performed entirely via data binding of IBindingList-s to UI components, rather than any of business logic, so logging such actions manually "in the same code" is not an option. The code only intercepts events when the user requests stuff to be updated in persistent storage.
I am writing a custom logger implementation that traverses the set of pending changes and composes log messages, which are in turn specific to change action (insert/delete/update) and object type. For certain object types I need to obtain and log specific data from relations. E.g. I have record types Foo and Bar - the type Foo has field bar of type Bar, which is essentialy an N-to-1 relation. Suppose I have the code below:
Code: Select all
var changes = context.GetChangeSet();
foreach (var c in changes.All)
{
...
if (changes.Deletes.Contains(c))
{
Foo foo;
...
if ((foo = c as Foo) != null)
{
Write(string.Format("[{0}] Deleted \"{1}\" from \"{2}\".",
DateTime.Now, foo.name, foo.bar.name));
}
...
}
...
}
...
context.SubmitChanges();
Is there a way to obtain the Bar name that would work in this case? All data manipulations are performed entirely via data binding of IBindingList-s to UI components, rather than any of business logic, so logging such actions manually "in the same code" is not an option. The code only intercepts events when the user requests stuff to be updated in persistent storage.