How to save record to relational tables using Linq To Sql

I have three tables:

Articles IdArticle Title Content

Tags IdTag TagName

ContentTag IdContentTag Idtag IdContent

When a user on my site writes an article with tags and submissions, I want to save it in the tables above.

Traditionally I have used transaction and I could do it. But how to do this using linq to sql?

0


source to share


2 answers


I think you are looking for something like this.



http://hookedonlinq.com/LINQtoSql5MinuteOverview.ashx

+1


source


Start by defining a linq to sql mapping. Let's say you named 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 am assuming the contentTag is a foreign key to the other two tables.

See Transaction Scope .

+1


source







All Articles