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

  • SEARCH
  • Home
  • 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 7876273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:08:34+00:00 2026-06-03T03:08:34+00:00

I have a basic question about implementing a Entity code first one to many

  • 0

I have a basic question about implementing a Entity code first one to many db. I have two models, Articles and Comments like this:

public class Article
    {
    public int Id { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
....

 public class Comment
{
    public int Id { get; set; }

    public virtual Article CommentForArticle { get; set; }

    public DateTime TimeStamp { get; set; 
....

If I try to add the Comment to the Db (with the Comment.CommentForArticle containing the Article) The comment is added to the database but Article does not contain the Comment in it’s collection of comments.

But if I create a comment, and add it to the article everything works fine. I didn’t have to add the comment to the database, it is added automatically (I guess.)

I was having problem after problem trying to implement this (errors like: Multiplicity constraint violated) so my question is, am I doing this correct now and why do you have to do it this way, not adding the Comment to the Db but to the Article that contains the comment. And can you share any link that may help me to better understand how the Entity Framework works with one to many and many to many relationships.

Thanks,
Garrett

  • 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-06-03T03:08:35+00:00Added an answer on June 3, 2026 at 3:08 am

    I would prefer to add new items to an owner’s collection if there is a “has” relationship. In your case, Article has Comments, so I would add a new Comment to article.Comments.

    An “is” relationsip, other than inheritance, is also possible. Suppose you had ArticleType entities and a property article.ArticleType. In this case, I would prefer to set the property. I would not even like ArticleType to have an Articles navigation property.

    In case of “pure” many-to-many (i.e. when the join table is not mapped as an entity type of its own) there is no other alternative than adding items to owner’s collections.

    It is not clear why you had problem after problem. Is it because you mixed both approaches? (adding Comments to the db and adding the same comments to the article).

    As for a reference: a great series of articles starts here. It focusses on more complex scenarios, but references to regular case are not hard to find.

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

Sidebar

Related Questions

first post, so play nice! I have a fairly basic question about Python dictionaries.
I have BASIC logical question about the following code snippet: 1 uint64_t RMTileKey(RMTile tile)
I'm a newbie and I have a very basic question about PHP arrays Code:
I've got a basic question about obj-c coding, say I have two classes represented
I have a basic question about the form action. Consider the below code here,
I have very basic question about the use of high performance cluster in our
I have a basic question about assembly. Why do we bother doing arithmetic operations
I might have pretty basic question about regex. I have the following regex, which
I am making a webapp. I have a fairly basic question about javascript performance.
Sorry about the wording for my question title. I have a basic HTML anchor

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.