How to access navigation property of deleted entity

In this post I am going to show you a small Entity Framework code first trick. When you remove an entity from the context its state is set to Deleted and all its navigation properties are nulled. This is done by DbContext internally. However you might want to know what was the related entities (navigation properties) before the entity was deleted. For instance you want to create some audit logs of deleted entity. I will show you how to do it with minimal effort.

By Mirek on (tags: Code First, Entity Framework, navigation property, categories: code)