LINQ to SQL Update EntitySet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top