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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:55:06+00:00 2026-06-09T14:55:06+00:00

I have User: -ID -Name Badge -ID -Name (E.G User has made 100 posts,

  • 0

I have

User:
 -ID
 -Name

Badge
 -ID
 -Name (E.G "User has made 100 posts", the same relationship as Stackoverflow)

So I’ve realised I need a third table, (as badges are defined in the DB aswell so I’ve created:

UserBadge
 -ID
 -User
 -Badge

So now in my User class I have

List<UserBadge> Badges;

Problem is, I’m going

Users.Find(1).Badges.Where(x => x.User == user && x.Badge = badgeIWant)
     .FirstOrDefault();

(Or something similar) Because… EF doesn’t have the intelligence (I’m guessing) to automatically know that “Badges” should only include the ones where the User matches.

How should I do this? Was thinking I could have a custom getter for Badges, that has that LINQ conditional in it…

  • 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-09T14:55:07+00:00Added an answer on June 9, 2026 at 2:55 pm

    This is EF 4.3 code first:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace ConsoleApp
    {
        public class User
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public virtual ICollection<Badge> Badges { get; set; }
        }
    
        public class Badge
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public virtual ICollection<User> Users { get; set; }
        }
    
        public class Context : DbContext
        {
            public Context()
                : base("Votes")
            {
    
            }
            public DbSet<User> Users { get; set; }
            public DbSet<Badge> Badge { get; set; }
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
                Context context = new Context();
                User user = context.Users.Include("Badges").Where(u => u.ID == 1).SingleOrDefault();
                if(user != null)
                {
                    //write your code.
                }
    
                Console.ReadKey();
            }
        }
    }
    

    I’ve tested it and this should work. This would typically be a many to many relationship because User can have many badges and 1 badge can belong to many users. If you run this code, you will find EF creates BadgeUsers table with BadgeID and UserID as foreign keys. From there on you can query and EF knows which badges you talking about.

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

Sidebar

Related Questions

I have User table in my DB. A user has the fields name, company_id
I have a <%= Html.TextBoxFor(user => user.Name) %> and it has standart width. What
I have user table ID Name 1 Michael 2 Jimmy if i do a
User has many Relationships In my relationships table i have a field :referee containing
I need to have one table in which i can set user preferences I
I have a user control which displays the currently logged in user's name. I
I have the following model: class User: name = models.CharField( max_length = 200 )
I have mail server configuration data (server - user name - password - port)
On websites where you have to enter a user name and password, I notice
Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull

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.