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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:04:01+00:00 2026-05-23T04:04:01+00:00

This is probably just because my knowledge with the EF Code First fluent API

  • 0

This is probably just because my knowledge with the EF Code First fluent API is lacking, but I’m stumped.

I want to model the following:

  • A Groups collection with Id and Name
  • A Users collection with Id and Name
  • Each user is assigned to exactly one primary group
  • Each user may have zero or many secondary groups

The table structure I’m going for would look like:

Groups

  • Id
  • Name

Users

  • Id
  • Name
  • PrimaryGroupId

SecondaryGroupAssignments

  • UserId
  • GroupId

I’ve been beating my head against a wall trying to model this with EF Code First, but I can’t get it to accept both relationships between User and Group. Sorry for not posting any .NET code (I’m happy to), but it’s probably all wrong anyway.

Is there a way to make EF model this? I’m assuming I have to do some sort of configuration with the Fluent API. Maybe a better question is: is there any good, definitive reference for the Fluent API?

Thanks!

  • 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-23T04:04:02+00:00Added an answer on May 23, 2026 at 4:04 am

    Try this (untested):

    public class Group 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public virtual ICollection<User> PrimaryUsers { get; set; }
        public virtual ICollection<User> SecondaryUsers { get; set; } 
    }
    
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int PrimaryGroupId { get; set; }
    
        public virtual Group PrimaryGroup { get; set; }
        public virtual ICollection<Group> SecondaryGroups { get; set; }
    }
    
    public class Context : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Group> Groups { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<User>()
                        .HasRequired(u => u.PrimaryGroup)
                        .WithMany(g => g.PrimaryUsers)
                        .HasForeignKey(u => u.PrimaryGroupId)
                        .WillCascadeOnDelete(false);
    
            modelBuilder.Entity<User>()
                        .HasMany(u => u.SecondaryGroups)
                        .WithMany(g => g.SecondaryUsers)
                        .Map(m => m.MapLeftKey("UserId")
                                   .MapRightKey("GroupId")
                                   .ToTable("SecondaryGroupAssignments"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question is probably stoopid. But I just don't want to cause
This is probably a pretty basic question, but just something that I wanted to
This is probably quite simple, but I just don't know how to do this...
This is probably a no brainer but... I've just opened a solution in VS2008
Ok this is probably just me not knowing enough about php but here it
This is probably just wishful thinking... Is there any way to check to see
I probably just haven't thought this through, or perhaps I'm simply unaware of an
This is probably going to be a really simple fix... I just can't figure
This is a fairly common problem, it probably has a name, I just don't
This probably has a simple answer, but I must not have had enough coffee

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.