views:

116

answers:

2

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?

A: 

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.

Adrian Grigore
I know, I'm actually not using the linqtosql models directly, I'm wrapping them up in separate classes, I want it to be independent from linqtosql and also offer nhibernate support etc.
kitsune
+2  A: 

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.

Phil Bennett
I realised too late that my answer was, in fact, almost a duplicate answer to Phil's answer here. +1 from me
RobS