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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:33:20+00:00 2026-06-17T16:33:20+00:00

My application will have Users and Groups. A User can be a part of

  • 0

My application will have Users and Groups. A User can be a part of more than one Group. A Group can also have more than one User.

I have been using this as a reference. In particular, the part near the bottom labeled “Many Relationship”. The difference between that example and my project is that my table already exists and I need to map my models to it. I suspect I should be able to remove my primary key from the Membership table and it might work (since it closer matches the example), but I would like to make sure before I mess with the database tables. It’s also very likely that my Fluent is totally wrong.

EDIT

As I am further researching the answer to this question, I stumbled upon a few things that may enhance the quality of the answers provided. I want to note that it is required to be able to add a User, Usergroup, and Membership independently.

SQL

CREATE TABLE TestDB.[UserGroup]
(
    GroupID int identity not null,
    Name varchar(100) not null,
    Comment varchar(1000)

    PRIMARY KEY(GroupID)
)
CREATE TABLE TestDB.[User]
(
    SomeID int not null,
    FirstName varchar(100) not null,
    LastName varchar(100) not null,
    Middle varchar(1) not null,
    Email varchar(100) not null,
    Phone varchar(16) not null,
    Comment varchar(1000)

    PRIMARY KEY(SomeID)
)
CREATE TABLE TestDB.[Membership]
(
    MembershipID int identity not null,
    GroupID int not null,
    SomeID int not null

    PRIMARY KEY(MembershipID)
    FOREIGN KEY(GroupID) references TestDB.[UserGroup](GroupID),
    FOREIGN KEY(SomeID) references TestDB.[User](SomeID)
)

NOTE:SomeID is named as such because the ID is given via another system. The database will not automatically generate this particular ID. It is guaranteed to be unique.

Model

public class UserGroup
{ 
    public int GroupID { set; get; }
    public string Name { set; get; }
    public string Comment { set; get; }

    public virtual ICollection<User> Users { set; get; }
}
public class User
{
    public string SomeID { set; get; }
    public string FirstName { set; get; }
    public string LastName { set; get; }
    public string Email { set; get; }
    public string Phone { set; get; }
    public string Comment { set; get; }

    public virtual ICollection<UserGroup> UserGroups { set; get; }
}

Fluent Mapping

modelBuilder.Entity<User>().ToTable("User", "TestDB");
modelBuilder.Entity<User>().HasKey(r => new { r.SomeID });
modelBuilder.Entity<User>()
            .HasMany(r => r.UserGroups)
            .WithMany(r => r.Users)
            .Map(m =>
                {
                    m.MapLeftKey("SomeID");
                    m.MapRightKey("GroupID");
                    m.ToTable("Membership");
                });

modelBuilder.Entity<UserGroup>().ToTable("UserGroup", "TestDB");
modelBuilder.Entity<UserGroup>().HasKey(r => new { r.GroupID });

Current Issue
Code has been updated to reflect what I currently have. I am able to query both Users and UserGroups independently, but it will not allow me to access the Navigation Properties.

  • 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-17T16:33:21+00:00Added an answer on June 17, 2026 at 4:33 pm

    I figured it out. My initial issues were quite silly and I was a lot closer than I had thought. One of those silly mistakes was actually mapping two entities to the same table which broke every call to that database.

    The first thing you need to do is make sure all your Navigation properties are virtual.

    public virtual ICollection<User> Users { set; get; }
    

    From here you need to figure out the Fluent API mapping. I had initially forgotten to include the schema for ToTable, which was not allowing me to traverse from User to UserGroup. In an instance where you are not working on an existing database, the .Map block would not be required. A table would be generated automatically containing Keys SomeID and GroupID. The table name will be the result of the two child table names concatenated. Read more here. When working with an existing database, this will simply map that table it would have created to one you already did.

    modelBuilder.Entity<User>()
            .HasMany(r => r.UserGroups)
            .WithMany(r => r.Users)
            .Map(m =>
                {
                    m.MapLeftKey("SomeID");
                    m.MapRightKey("GroupID");
                    m.ToTable("Membership", "TempDB");
                });
    

    Another Great Source

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

Sidebar

Related Questions

In my web application I will have users, each user will have albums, and
I am creating a web application that will have many users. Each user has
My application will have a per machine (not per user) Startup shortcut. I can
I’m building an application where different users will have different menu items available to
I have an application that will mail users attachments of text files. Receiving users
We have an application that will capture different images from users and make them
When no users have the any pages in an application open, it will unload
I have a silverlight application which users will be running in various time zones
I'm working on an application for iOS which will have the user fill out
I am looking to build an application where the user will have the ability

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.