MVC2.NET 从视图模型传递对象来查看和访问它
当使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
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.