When Entity Removed the Foreign Key is not recorded
See original GitHub issueHi,
I have found a minor issue in DbContextHelper.UpdateAuditEvent not recording the foreign keys when an entity has been removed. When the entity is removed, the foreign keys are removed by EF and the object is essentially nulled out except for the primary key. When the DbContextHelper method UpdateAuditEvent is called, it calls GetForeignKeys for which the dictionary will have null values for the keys. During the foreach loop to update the efEntry.ColumnValues, the fk.Value will be null even though the column value has a perfectly usable foreign key identifier.
I can submit a pull request if I get time other wise
efEntry.ColumnValues[fk.Key] = fk.Value
could be changed to
efEntry.ColumnValues[fk.Key] = fk.Value ?? efEntry.ColumnValues[fk.Key]
Thanks
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top GitHub Comments
Please reference latest version 12.1.8 and try again. Added the workaround to avoid updating the foreign column values from the foreign key values that are set to NULL by EF.
Re-open if you’re still having this problem.
Oh I see…
Looks like this is happening only on EF6. Maybe a bug on the EF code? I cannot be sure, but I will provide a new version with a NULL check on the foreign key value before overriding it, like in the OP.