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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:43:16+00:00 2026-05-23T14:43:16+00:00

I have a self-referencing many-to-many relationship between users and managers that looks like this:

  • 0

I have a self-referencing many-to-many relationship between users and managers that looks like this:

 public class User
    {
        public virtual Guid UserId { get; set; }
        public virtual string Username { get; set; }    
        public virtual ICollection<UsersManagers> Managers { get; set; }
    }

public class UsersManagers
{
    public int UsersManagersId { get; set; }
    public virtual Guid ManagerId { get; set; }
    public virtual Guid UserId { get; set; }
    public int ManagerRank { get; set; }

    [ForeignKey("ManagerId")]
    public virtual User Manager { get; set; }
}

So users can have a number of managers that can be sorted by rank. When I seed my database and a user with a few managers, like this:

        var user = u.All.Where(e => e.Username == "Neil").FirstOrDefault(); 

        UsersManagers c = new UsersManagers {ManagerRank = 1, ManagerId = manager1.UserId, UserId = user.UserId};
        UsersManagers c1 = new UsersManagers { ManagerRank = 2, ManagerId = manager2.UserId, UserId = user.UserId };

Everything is created properly in the database. There are 3 entries in the UsersManagers table with the correct UserId, ManagerId and ManagerRank.

When I load the user from my repository and try to access the Managers property, the collection is empty. What can I do to fix this?

Any help would be appreciated.

Update:

I’ve added including the Managers when loading Users as suggested below. EF is now returning the wrong mangers for the wrong users. Here is the data from the UsersManagers table, it should be returning 4 managers for 1 user but it is returning 1 manger for 4 of the users.

UsersManagersId        ManagerId            UserId                       ManagerRank
1   2157B648-7FE3-4784-A742-687682672EE8    F603EB04-FF22-4E8E-8FB2-3AA12F3F6C8F    1
2   862C2E56-8DF2-4110-B6E4-534B8C0E8F75    F603EB04-FF22-4E8E-8FB2-3AA12F3F6C8F    2
3   A95AD9A2-6475-4B4C-925A-6C0522E9B004    F603EB04-FF22-4E8E-8FB2-3AA12F3F6C8F    3
4   A6D37381-2507-46E8-B84D-059A1B4F1020    F603EB04-FF22-4E8E-8FB2-3AA12F3F6C8F    4

Solution

Solution was to change the ForeignKey annotation for Manager in UsersManagers to use “UserID”.

  • 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-23T14:43:16+00:00Added an answer on May 23, 2026 at 2:43 pm

    Here is a sample code bit that loads all Sessions and related Trackings for a Program in my repository.

    public Program GetById(int id)
    {
        var entity = _context.Programs
            .Include(p => p.Sessions.Select(s => s.Trackings))
            .Single(p => p.ID == id);
    
         return entity;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Stumped on this one. I have a many to many self referencing relationship(FluentNH) mapped
I have a structure that looks following Class TreeNode { public TreeNode Parent {
i have a self referencing table called project that has a master detail relationship
I have a table in my source DB that is self referencing |BusinessID|...|ParentID| This
I have a self referencing table named categories that has a parentcategoryid column that
I have a self referencing table in Oracle 9i, and a view that gets
I'd like to create a self referencing relation in rails. I have a Person
I have a self-referencing table (Customers) and a table that will link to one
I have a self referencing table and I want a mysql query that will
I have a model with self-referencing ManyToMany field: class Suite(models.Model): suites = models.ManyToManyField(self, related_name=parents,

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.