一对多关系不起作用 - 实体框架
我在实体框架(我相信是 3.5)中创建一对多(或一对一?)关系时遇到问题。
示例表/模型:
Settings:
SettingsID pk int not null
SettingsName varchar(250) null
SettingsTypeID fk int null
SettingsType:
SettingsTypeID pk int not null
SettingsTypeName varchar(250)
我对引用 SettingsType.SettingsTypeID
的 Settings.SettingsTypeID
有一个外键约束。
保存设置(使用选定的settingstype
)后,值会正确保存(我已检查数据库以确保并且可以看到Setting.SettingsTypeID
的值正确更新)。
但是,在尝试根据所选的 Setting
检索 settingType
对象时,例如
var SettingsType = Setting.SettingsType;
Setting.SettingsType
总是返回 null
?
我错过了什么吗?
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
, e.g.
var SettingsType = Setting.SettingsType;
Setting.SettingsType
always comes back null
?
Am I missing something or?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用本文中描述的加载模式之一来加载与您的
Setting
对象关联的SettingType
对象。 microsoft.com/en-us/library/bb896272.aspx" rel="noreferrer">加载相关对象 (MSDN)。我建议使用
Include
方法 ,像这样:You need to load the
SettingType
object that is associated with yourSetting
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: