I have an entity A that used as navigation property for another entity B. I would like to control the Insertion for entity A. Thats meen that whenever I would insert A I have to perform other checks and update other entities.
But when I Insert entities of type B it automatically inserts the connected A entities without the extra checks and updates I need.
How can I solve this?
UPDATE
I decided to use this answer as suggested. But in the OnBeforeInsert() I might add a new entities to the context which their OnBeforeInsert() won’t be called since in the time var changedEntities = ChangeTracker.Entries(); was called the new entitied wasn’t exist yet.
How can I solve this?
EF has very few extension points. So it is sometimes very difficult to customize.
This answer is an extension of my previous answer
Then in your
DbContextclass subscribe toObjectStateManagerChangedevent ofObjectStateManager.If you are using EF 4.0 you will need to customize this accordingly.