从linq到sql访问关联对象时遇到问题.我有一篇文章和用户.每篇文章都有一个卖家(用户),每个用户都有很多文章.我通过协会解决了这个问题.
这是我的linq到sql类的样子:

这是协会:

以下是Article.Seller背后的代码:
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="User_Article", Storage="_Seller", ThisKey="SellerID", OtherKey="ID", IsForeignKey=true)]
public User Seller
{
get
{
return this._Seller.Entity;
}
set
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我想要获得文章的卖家时,我收到以下错误:
无法访问已处置的对象.对象名:'Dispose后访问的DataContext.'.
错误发生在卖家的获取中.
任何想法如何处理这个?
编辑:Heres'使用DataContext的代码:
public static List<Article> Read()
{
using (uDataContext dbx = new uDataContext())
{
return dbx.Article.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
该列表使用如下:
List<Article> articles = ArticleDALC.Read();
foreach (Article article in articles)
{
// Exception appears here!
User seller = article.Seller;
....
}
Run Code Online (Sandbox Code Playgroud)
Nag*_*far 17
解决方案:
DeferredLoadingEnabled使用DataContext时,只需将属性设置为false:
public static List<Article> Read()
{
using (uDataContext dbx = new uDataContext())
{
dbx.DeferredLoadingEnabled = false;
return dbx.Article.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11489 次 |
| 最近记录: |