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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:04:30+00:00 2026-06-14T11:04:30+00:00

Sorry but i’m a beginner. I have two classes: User and Group which are

  • 0

Sorry but i’m a beginner.

I have two classes: User and Group which are linked together (Group class have an User type attribute)

Here is User.cs :

public class User
{
    [Key]
    public int userId { get; set; }

    [Required]
    [MaxLength(10)]
    [Display(Name="Firstname")]
    public string firstname { get; set; }

    [Required]
    [MaxLength(10)]
    [Display(Name = "Lastname")]
    public string lastname { get; set; }
}

Here is Group.cs :

public class Group
{
    [Key]
    public int idGroup { get; set; }
    public string name { get; set; }
    public User owner { get; set; }
}

Here is the insert

private myContext db = new myContext();
[HttpPost]
    public ActionResult Create(Group group)
    {
        group.owner = (User)Session["user"];
        if (ModelState.IsValid)
        {
            db.Groups.Add(group);
            db.SaveChanges();
            return RedirectToAction("Index","Home");
        }

        return View(group);
    }
//Session["user"] contains an User type object (which is already in database)

The problem is : When I add a new group, it automatically inserts a new User into the databae, instead of JUST inserting the corresponding group.

Example : I am “user1” (so “user1” is present in the User table) and I want to create the “group1” group.
I link the user I am with the “owner” attribute of the Group class.

group1 will be inserted into Group table but user1 will be also inserted into the User table.. That will cause a duplicate content

I don’t know how to avoid this…

Any help would be appreciated.

  • 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-14T11:04:31+00:00Added an answer on June 14, 2026 at 11:04 am

    You’ll have to attach your existing User to your DbContext instance using Attach. This will prevent EF to create a duplicate of the object.

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

Sidebar

Related Questions

Sorry for repost but I have a better working example of data that reflects
I'm very sorry but after a few hours of trying I just have to
Sorry but I dont know what title I should give this question(Suggestion?) I have
I'm sorry but there can't be an example so we'll have to take a
Sorry but iam an JS Newbie (-: I have to animate some span tags
First of all I'm sorry but I'm a really big beginner. I dont really
I´m sorry but I have to ask a very simple question. My problem is
Sorry but I found no clear answer on that. I have an iphone app
sorry but I do not have the actual code with me, but I will
Sorry but I am not sure how to ask this question but I am

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.