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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:52:38+00:00 2026-06-14T10:52:38+00:00

I have a problem with relationships in Entity-framework Here is my problem : I

  • 0

I have a problem with relationships in Entity-framework

Here is my problem :

I have two classes : User and Group

  • A group must have ONE Owner (User)
  • A group can have 0 or severals Members (User)
  • An User can be the owner of 0 or severals group

User.cs :

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


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


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


    [Display(Name = "Email")]
    public string email { get; set; }

}

Group.cs :

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

    public Group()
    {
        members = new List<User>();
    }

}

And here is the insert group function :

        [HttpPost]
    public ActionResult Create(Group group)
    {
        if (ModelState.IsValid)
        {
            group.owner = db.Users.Attach((User)Session["user"]);
//Current user stored in session and already presents in User table
            db.Groups.Add(group);
            db.SaveChanges();

            return RedirectToAction("Index", "Home");
        }
        return View(group);
    }

The problem is :

  • When I run debugging into the insert function, the “owner” attribute is correctly set (at db.savechanges)
  • But after, when I select a group in db (Group group =
    db.Groups.Find(id);), the “owner” parameter is null.

Any help would be really appreciated.

Thanks a lot

  • 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-14T10:52:40+00:00Added an answer on June 14, 2026 at 10:52 am

    Try this:

    public class Group
    {
        [Key]
        public int idGroup { get; set; }
        public string name { get; set; } 
        public virtual User owner { get; set; }
        public virtual ICollection<User> members { get; set; }
    
        public Group()
        {
            members = new List<User>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the entity framework to take data (nested relationships) from one
Here is my problem with simplified example. I have two entities modeled from SQL
Problem is next - i have 2 entities with one-to-many relationships: public class Schema
I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate
I have run into an interesting problem with Entity Framework and based on the
I have a problem With Entity Framework and TPC Inheritance With Self-Referencing Child Class.
I have problem with searching. So, I have Task entity and Owner Entity. They
I have a problem updating a foreign key in an Entity Framework entity. I
My Question is can we extend one context to another in Entity Framework 4.
I am having trouble with foreign key relationships in the entity framework. I have

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.