"Unable to access the remote object"

I have a problem accessing an association object from linq to sql. I have a class and user article. Each article has a seller (which is the User), and each user has many articles. I solved this with the help of an association.

This is how my linq to sql classes look like this: linq to sql classes

And this is the association:

association

Here is the code behind 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 { ... } } 

Now, when I want to get the Article Seller, I get the following error:

Unable to access the remote object. Object Name: 'Access to the DataContext after Dispose. '.

The error occurs when receiving the seller.

Any ideas how to handle this?

EDIT: Heres is the code that uses the DataContext:

 public static List<Article> Read() { using (uDataContext dbx = new uDataContext()) { return dbx.Article.ToList(); } } 

The list is used as follows:

 List<Article> articles = ArticleDALC.Read(); foreach (Article article in articles) { // Exception appears here! User seller = article.Seller; .... } 
+6
source share
2 answers

Solution found:

Just set the DeferredLoadingEnabled property to false when using the DataContext:

 public static List<Article> Read() { using (uDataContext dbx = new uDataContext()) { dbx.DeferredLoadingEnabled = false; return dbx.Article.ToList(); } } 
+15
source

Do not delete your DataContext.

All LINQ objects are associated with a DataContext. You are probably accessing the object outside the using block, where the DataContext is created.

+4
source

All Articles