As I understand it (so please correct me if I am mistaken), entity classes that have relationships between them can be defined in several ways -- (1) by using both a property to represent a scalar key plus a navigation property or (2) just using a navigation property.
If you use Data Annotations I believe that your only choice is using both the scalar key and navigation properties whereas using Fluent API both approaches are available.
My question is which approach to take? What are the pros and cons of each? (This is in regard to the two choices regarding foreign keys and navigation properties...not regarding Data Annotations vs. Fluent.
As I see it it boils down to these factors/concerns
- Having both a scalar property and a navigation property could be seen as "dirtying up" the class.
- Having just the navigation property would require an Includes() call to get access to the FK value. I have not looked at the SELECT statement being sent but I would suspect that a JOIN is being executed and data that is not needed is being sent back.
- Having the scalar property would preclude the need for Include() calls.
- As hinted in a previous SO question from me (related question), it looks like navigation properties cannot be used as part of a composite key so one would be forced to include the additional scalar key. And if you have to do it for one property it would make sense to be consistent in all your classes.
Am I missing anything? In general, what is the recommended way of defining the foreign keys and navigation properties? Why even offer both approaches as it seems to muddle the waters? I have looked at a lot of tutorials and see a mix of both approaches.
Thank you.