Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 731105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:59:18+00:00 2026-05-14T06:59:18+00:00

I have a function where I can add articles and users can comment on

  • 0

I have a function where I can add articles and users can comment on them. This is done with a one to many relationship like= "commentId=>ArticleId". However when I try to add the comment to the database at the same time as I add the one to many record, the commentId is not known. Like this code:

Comment comment = new Comment();
comment.Date = DateTime.UtcNow;
comment.Text = text;
comment.UserId = userId;
db.Comments.InsertOnSubmit(comment);
comment.Articles.Add(new CommentsForArticle() { ArticleId = articleId, CommentId = comment.CommentId });

The commentId will be 0 before i press submit. Is there any way arround not having to submit in between or do I simply have to cut out the part where I have a one-to-many relationship and just use a CommentTable with a column like "ArticleId".

What is best in a performance perspective?

I understand the underlying issue, I just want to know which solution works best.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:59:18+00:00Added an answer on May 14, 2026 at 6:59 am

    You’re asking two different questions.

    First, if a comment can only be for one article, it makes much more sense from a database design perspective to have ArticleId in the Comment table rather than creating a link table. An intermediate link table is typically only used for many:many relationships, since they can’t be modeled explicitly and need that level of indirection.

    That being said, if you have your foreign keys defined properly, then Linq2SQL will detect them and allow you to work with objects rather than keys (or you can use either). For example, your last line could be something like this, assuming you left your database design as-is:

    comment.Articles.Add(new CommentsForArticle() { ArticleId = articleId, Comment = comment }
    

    Notice that I’m using the actual Comment object rather than its ID. Doing it this way, Linq2SQL can automatically do that mapping for you. If you have a handle to the actual article, you could replace ArticleId = articleID with Article = article as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that allows the user to add information an their leisure.
I have a basic app using the ASP.NET membership provider. By default, you can
Traditionally, to call a JavaScript function once the page has loaded, you'd add an
I have a 3rd party server side control that generates JavaScript and I'd like
I'll try and keep my sample code very straightforward, but it may have errors
I found this article: http://www.deluxeblogtips.com/2010/05/editor-style-wordpress-30.html I created a child theme using the Twentyten theme
I am new to C#. I have a Persons class and a User class
I have an email form. But I'm making a test email form where a
Fairly new to jQuery and JavaScript in general. I mocked up an example of
I'm an ASP.NET newbie, but not so new at programming in general. I'm creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.