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 7749979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:10:19+00:00 2026-06-01T11:10:19+00:00

In this website, users can register under a username and password, and can also

  • 0

In this website, users can register under a username and password, and can also post comments on articles. The models are pretty straightforward:

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public bool IsAdmin { get; set; }
    public DateTime JoinDate { get; set; }
    public string AvatarPath { get; set; }
    public string EmailAddress { get; set; }
}

public class ArticleComment
{
    public int Id { get; set; }
    public int ArticleId { get; set; }
    public int UserId { get; set; }
    public string CommenterName { get; set; }
    public string Message { get; set; }
    public DateTime CommentDate { get; set; }
    public User User { get; set; }
}

Entity Framework correctly made the foreign key relationship between UserId on ArticleComment and Id on User when the database was created using code-first.

Here’s my code for when a user posts a new comment:

public JsonResult SubmitComment(int articleId, string comment)
    {
        var response = new JsonResponse();
        var currentUser = _userRepository.GetUserByUsername(User.Identity.Name);

        //...

        var newComment = new ArticleComment
        {
            ArticleId = articleId,
            CommentDate = DateTime.Now,
            CommenterName = currentUser.Username,
            UserId = currentUser.Id,
            User = currentUser,
            Message = comment,
        };

        try
        {
            _articleRepository.Insert(newComment);
        }
        catch (Exception e)
        {
            response.Success = false;
            response.AddError("newComment", "Sorry, we could not add your comment. Server error: " + e.Message);
            return Json(response);
        }

        response.Success = true;
        response.Value = newComment;
        return Json(response);
    }

The values that make up the newComment object all appear to be correct, and the Insert method in my Article repository class is straight and to the point:

    public void Insert(ArticleComment input)
    {
        DataContext.ArticleComments.Add(input);
        DataContext.SaveChanges();
    }

But once this happens, poof: a new record in my Users table appears along with the new record in ArticleComments. All of the info in the new Users record is duplicated from that user’s existing record – the only difference is the value for the primary key Id. What gives?

  • 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-01T11:10:21+00:00Added an answer on June 1, 2026 at 11:10 am

    In addition to my comment, you need to make sure that both _userRepository and _articleRepository are using the same DbContext instance.

    Either that, or you can try this:

    var newComment = new ArticleComment
    {
        ArticleId = articleId,
        CommentDate = DateTime.Now,
        CommenterName = currentUser.Username,
        UserId = currentUser.Id,
        // User = currentUser, let the UserId figure out the User, don't set it yourself.
        Message = comment,
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a CodeIgniter site that registers users through this url: http://website.com/user/register/1 Unfortunately,
I use symfony 1.4.8 There are website on it where users can register. I
i am developing a dating website, in which users can register from all over
I have a internal website that users log into. This data is saved as
In Wordpress MU, how can I allow users to register for new blogs with
I have a website. Visitors can register. Every user can send messages to another
I have a system in which users can register with my forum via the
Currently on my website, users login with their login id and password, they are
Okay, I got a website where you can register yourself and login. You can
Our website lets users create small widgets that can be embedded on other third

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.