Yes, I think there is no need for foreign key properties and they are somehow a relational artifact in the object world. You can define relationships completely without FK properties. In Fluent API you can define if the relationship is optional or required and you can specify the database table's foreign key column name. The relationship is then called Independent Association.
My understanding is that Foreign Key Associations - relationships with exposed foreign key properties in your model class - only exist to make working with relationships in Entity Framework a bit easier and more comfortable in certain scenarios. For example:
Suppose you have a web view to create or edit a product and the view contains a combobox to select a category and assign it to the product. To fill the combobox when the view is rendered you would load, for instance, the ID
and the Name
of all categories from the database.
When the page is posted back you would receive the properties for the product and the ID
of the selected category. If you don't have a foreign key property CategoryID
in your Product
you would have to create the relationship this way:
var category = new Category { ID = IDFromComboBox };
context.Categories.Attach(category);
product.Category = category;
With a FK property you only need one line:
product.CategoryID = IDFromComboBox;
Foreign key properties didn't exist in Entity Framework version 1 (.NET 3.5) and have been introduced with EF version 4 (.NET 4) to support scenarios like the above better.
A critical view on Foreign Key Association can be found and the difference between the two types of associations is very well discussed in Ladislav's blog:
http://www.ladislavmrnka.com/2011/05/foreign-key-vs-independent-associations-in-ef-4/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…