views:

381

answers:

2

I have three tables like that:

Articles IdArticle Title Content

Tags IdTag TagName

ContentTag IdContentTag Idtag IdContent

When user in my site write an article with adding tags and submit, I want to save it to tables above.

In traditional ways, I used to use transaction and I could do it. But How can I do it by using linq to sql?

+1  A: 

I think you're looking for something like this.

http://hookedonlinq.com/LINQtoSql5MinuteOverview.ashx

Andrew Theken
+1  A: 

Start by defining a linq to sql mapping. Let's assume you called it 'LtoS'.
Using it in code it will look something like this

using(var ts = new TransactionScope())
using(var dc = new LtoSDataContext())
{

  var _article = new article
  {
    Title="someTitle",
    Content="someContent"
  };
  dc.articles.InsertOnSubmit(_article);

  var _tag = new tag
  {
    TagName="someTagName"
  };
  dc.articles.InsertOnSubmit(_tag);

  var _contentTag = new contentTag
  {
    Tag = _tag,
    Article = _article
  };
  dc.articles.InsertOnSubmit(_tag);

  dc.SubmitChanges();
}

I have assume the contentTag is foreign key'ing into the other two tables.

See Transaction Scope.

Joel Meador