LINQ to SQL Update EntitySet

  • Thread starter Thread starter Miroslav Endys
  • Start date Start date
M

Miroslav Endys

What is the best way to update EntitySet in the object:

class Article
{

public string ID { get; set; }
public string Caption { get; set; }

public EntitySet<SourceCategory> SourceCategories

}

I retrieve Article by ArticleID

Article currentArticle =
(Article)MyDataContext.Current.tblArticles.SingleOrDefault(art => art.ID ==
wantedArticleID);

Well, now I have currentArticle and I want to change set of the source and
destination categories.

Should I manualy delete all related categories from their table and then
clear EntitySet and then add new categorya and SubmitChanges()???

var srcCats = MyDataContext.Current.tblCategories.Where(src => src.ArticleID
== currentArticle.ID);
MyDataContext.Current.tblCategories.DeleteAllOnSubmit(srcCats);
currentArticle.SourceCategory.Clear();
currentArticle.SourceCategory.Add(new SourceCategory("category1",
currentArticle.ID));
currentArticle.SourceCategories.Add(new SourceCategory("category2",
currentArticle.ID))
MyDataContext.Current.SubmitChanges();

Sometimes I get NullReferenceException. Seems the problem is in cleared
entities.
What is the best way to do that??
(the problem is more complicated, because SourceCategory is inherited)

Thaks for any ideas;
Mike Endy
 
Back
Top