Let's say I have a Project table with a FK CompanyId which relates the project to a company Table.
In your Project model, do you add a Company object, or just the CompanyId property and retrieve the Company when needed in code?
Let's say I have a Project table with a FK CompanyId which relates the project to a company Table.
In your Project model, do you add a Company object, or just the CompanyId property and retrieve the Company when needed in code?
When using LinqToSQL, foreign key relationships are automatically modelled in the business entities generated by SQLMetal. The entity types have a child or parent attribute named after the child or parent's type. You can use those to retrieve the related business entity.
These attributes support lazy loading, so you the respective child or parent entities are only loaded from the database when needed.
This depends on what you're going to do with it. If, say, you have a UI with all the fields needed to create a project, and in that UI you choose the company to associate the project with a drop down box then maybe just associating the key would be a reasonable efficient thing to do.
I would prefer to attach a company object in terms of "purity", but if I never had a case where I would actually do anything with that customer I would simply use the key. I don't know what language / platform your using but you could consider some sort of lazy loading scheme where the company object gets populated with its data on a JIT basis.