views:

29

answers:

1

Hi All,

I'm having a problem with creating a one-to-many(or one-to-one?) relationship in entity framework (3.5 I believe).

Example tables/models:

Settings:
    SettingsID pk int not null
    SettingsName varchar(250) null
    SettingsTypeID fk int null

SettingsType:
   SettingsTypeID pk int not null
   SettingsTypeName varchar(250)

I have a foriegn key constraint on Settings.SettingsTypeID that referances SettingsType.SettingsTypeID.

Upon saving a setting(with a chosen settingstype) the values save correctly (I have checked the DB to be sure and can see the value of Setting.SettingsTypeID update correctly).

However, upon trying to retrieve a settingType object based on the chosen Setting, e.g.

var SettingsType = Setting.SettingsType;

Setting.SettingsType always comes back null?

Am I missing something or?

+1  A: 

You need to load the SettingType object that is associated with your Setting object, using one of the loading patterns described this article on Loading Related Objects (MSDN).

I'd suggest using the Include method, something like this:

var setting = (from s in context.Settings.Include("SettingsType") 
               where s.SettingsID == id select s).FirstOrDefault();
Donut