实体框架可以通过 ID 自动更新属性导航吗

发布于 2024-12-12 18:19:55 字数 342 浏览 0 评论 0原文

在实体框架中,当我通过设计器将表映射到相应的实体并开始实际使用它们时,我会发现一个实体 - Thing,它具有关系(多对一)或一对一)与另一个对象(例如 Bob)会在 Thing 上生成以下三个属性:

Bob
BobId
BobReference

如果我设置 BobId< /code>,并保存我的实体,下次我获取此 Thing,我将能够毫无问题地导航 Bob 属性。不过,我很好奇是否可以配置 EF 以允许我导航属性而无需立即保存。

In Entity Framework, when I've mapped my tables to the corresponding entities through the designer and get to actually using them, I'll find that an entity - Thing, who has a relationship (many to one, or one to one) with another object, say, Bob, for example, would produce the following three properties on Thing:

Bob
BobId
BobReference

And were I to set BobId, and save my entity, the next time I fetch this Thing, I'll be able to navigate the Bob property without trouble. I'm curious, however, if it is possible to configure EF to allow me to navigate the property without having to immediately save.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沐歌 2024-12-19 18:19:56

你可以这样做:(EF 4.1)

//Has to exists a record on Bob table with Id = 1
var thing = new Thing() { BobId = 1 };

var context = new YouContext();
context.Entry(thing ).State = EntityState.Unchanged;
context.Entry(thing ).Reference(x => x.Bob).Load();

然后 thing.Bob is != null

You can do something like this: (EF 4.1)

//Has to exists a record on Bob table with Id = 1
var thing = new Thing() { BobId = 1 };

var context = new YouContext();
context.Entry(thing ).State = EntityState.Unchanged;
context.Entry(thing ).Reference(x => x.Bob).Load();

and then thing.Bob is != null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文