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 foreign key constraint on Settings.SettingsTypeID
that references 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
, eg
var SettingsType = Setting.SettingsType;
Setting.SettingsType
always comes back null
?
Am I missing something or?
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.