MVC2.NET 从视图模型传递对象来查看和访问它

发布于 2024-09-11 12:31:48 字数 357 浏览 6 评论 0原文

当使用 Viewmodels 从 linq 到 sql 时,我遇到了架构问题。

当您从数据库中有一个对象(比方说“人”),并将其加载到视图模型中时。在您看来,在此之后,当您尝试访问引用的类时(假设一个人有子对象,它是数据库中的不同表和不同的数据上下文对象)。您会得到一个“无法访问已处置的数据上下文对象(或其他内容)”,这是合乎逻辑的,因为您加载了对象并处置了数据上下文。例如:

“object” - > foreach(Child child in Model.Person.Children.ToList() ){}

但是,如何才能做到(“预加载”?)子级或访问它们,而无需创建新的数据上下文,并在 View 中重新加载 person 呢?

When working with Viewmodels from linq to sql I have an architectural problem.

When you have an object from your db (let's say "person"), and you load it in your viewmodel. After this in your view, when you try to acces referenced classes (let's say a person has children object which is a different table in db, and a different datacontext object). You get a "cannot access disposed datacontext object (or something) which is logical, because you loaded your object and disposed the datacontext. for example:

"object" ->foreach(Child child in Model.Person.Children.ToList()){}

But how can you make it so that you can ("preload"?) the children or acces them without having to create a new datacontext, and reload the person in your View ?

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

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

发布评论

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

评论(1

无可置疑 2024-09-18 12:31:48

使用 DataLoadOptions.LoadWith()

请参阅此处的示例:http://msdn.microsoft.com/en-us/library/Bb386917(v=VS.90).aspx

您必须指定 DataLoadOptions 并在在执行任何查询之前访问 DataContext 实例。

Use DataLoadOptions.LoadWith()

See examples here: http://msdn.microsoft.com/en-us/library/Bb386917(v=VS.90).aspx

You must specify DataLoadOptions and assign in to the DataContext instance before executing any queries.

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