question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When Entity Removed the Foreign Key is not recorded

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
thepirat000commented, Jul 2, 2018

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.

0reactions
thepirat000commented, Jul 2, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Entity Framework support when deleted a record and ...
If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the...
Read more >
3 common foreign key mistakes (and how to avoid them)
Dangling foreign keys 3. Not creating foreign key indexes Bonus: Not using foreign keys Key takeaway: think before you CREATE TABLE.
Read more >
Cascade Delete - EF Core
If the principal/parent entity is deleted, then the foreign key values of the dependents/children will no longer match the primary or ...
Read more >
Query about delete rule
If you set the rule to ignore and you delete the user, any records that had that user's foreign key are now 'orphaned',...
Read more >
Operating without foreign key constraints
Possibly the single most important feature of foreign keys, a DELETE on a parent will fail if child rows exist that reference the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found