Entity Framework Foreign Key
What is better? Declaring your foreign keys in your model and declaring it in your modelbuilder (.hasForeignKey) or doing it by only declaring the relationship in your modelbuilder?
3 Replies
i believe modelbuilder is what would conventionally be suggested
There is also a
ForeignKey
attribute that you can use
Means you don't need to configure it in your dbcontext at allPersonally, I use FKs and nav properties in my models
This way I can always construct a
Blogpost
with a related Author
without having to fetch the author first
And since it just works by convention, there's no configuration needed
Be it with attributes or modelbuilder