【发布时间】:2009-03-27 17:15:23
【问题描述】:
我正在做的工作,但这是错误的,我知道。
public Profile GetProfile(int id)
{
Profile profile = (
from u in en.User where u.UserID == id
select u.Profile.FirstOrDefault()
).First();
if (profile.UserReference.Value == null)
profile.UserReference.Load();
return profile;
}
Profile.UserID 是与 User.UserID 绑定的 FK。因此,实体框架不将 Profile.UserID 包含在实体模型中,这就是为什么我的 linq 查询首先查找 User 对象,然后选择相关的 Profile 对象(这对我来说很脏)。
谁能提供更好的解决方案:
- 通过 User.UserID 查找 Profile 对象。
- 在返回的 Profile 对象中加载 User 对象引用。
提前致谢!
【问题讨论】:
标签: c# linq entity-framework