views:

39

answers:

1

Having a bit of a mental block about how this should work:

I have two tables Location and Country. Within the Location table I have a FK to the Country table PK which is CountryCode.

The Country table is filled with static data and each time I add a new location I will link to the Country with the appropriate countryCode.

I can do this using the EF by querying to return the Country object for that country code each time I want to create a new location. This doesn't seem very efficient and there must be a better way, is there a way to link to the country object without performing the query since I know the key? or do I need to create a CountriesList object and query that in memory each time to avoid the db call? or is there another way?

+2  A: 

The way to set the relationship between Location and Country without querying the Country (if you know the Country key) exists:

newLocation.CountryReference.EntityKey = new EntityKey("YourContext.Country", "CountryID", countryID_Value);

Hope this helps.

Misha N.
This is the best solution for EF version 1. (+1) For EF version 4, use FK associations.
Craig Stuntz
Thanks, that works perfectly.
Richbits