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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:22:58+00:00 2026-05-26T17:22:58+00:00

I’m attempting to add a property in a partial class to an entity framework

  • 0

I’m attempting to add a property in a partial class to an entity framework model that includes entities that have been mapped via navigation properties unioned with some additional rows.

Database:
I have a Users table with a UserID primary key. Additionally, I have a Tags table with a OwnerUserID (foreign key, nullable), which is set to the UserID if it is user specific, but may also be null to indicate it applies to all users.

EF Model:
The navigation property Users.Tags is properly generated to include all tags that are explicitly assigned to that user.

Problem:
I need a property on the User entity that includes all tags that have the OwnerUserID of the User and those that are null.
So I want to be able to say something like:

public partial class User
    {
        public IEnumerable<Tag> VisibleTags
        {
            get
            {
                return tags = (from t in {AllTags}
                                where t.PrivateUserID == null
                                select t).Union(
                                from t in this.Tags
                                select t);
            }
        }
    }

…where {AllTags} represents all tags in the system.

  • 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-26T17:22:58+00:00Added an answer on May 26, 2026 at 5:22 pm

    A quick comment on the design:

    I am not a big fan of nullable values in a database, especially when the null provides contextual information, like the value applying to all users, as opposed to no users.

    You might want to consider a design where you can group users, and tags would be visible to a group of users. This would be more flexible in case your needs changed in the future, and more explicit. It also would make it so you can make the column non-nullable.

    If you don’t want to build that into your system now, I’d personally plot out what a migration from your current design to a group-based design would look like, just to make sure you didn’t paint yourself into a corner design-wise.

    The problem at hand

    You get AllTags from the context, so you’re going to have to use a context object.

    Attempt 1

    One way to do this would be to create a method on the context that accepts either a User or a UserId.

    // In partial Context class...
    
    public IEnumerable<Tag> GetVisibleTags(User user)
    {
        return Tags.Where(t => t.PrivateUserID == null)
            .Union(user.Tags)
            ;
    }
    
    // Call it like this...
    
    context.GetVisibleTags(someUser);
    

    I don’t completely like because I don’t think a context should be used to make queries. That is a job for the “repository” (entity/DbSet/DataSet class).

    Attempt 2

    Another way (which would make it accessible off the user) would be to add a context parameter to your getter method:

    // In partial User class...
    
    public IEnumerable<Tag> GetVisibleTags(MyContextClass context)
    {
        return context.Tags.Where(t => t.PrivateUserID == null)
            .Union(this.Tags)
            ;
    }
    
    // Call it like this
    
    someUser.GetVisibleTags(context);
    

    I like this even less than the first because a “repository” (entity/DataSet/DbSet) shouldn’t be allowed to know anything about a context.

    Attempt 3

    You could create a query wrapper object to solve this. I generally suggest you write complex queries this way when you can, so they are reusable.

    The trick is figuring out a good domain specific name name for the class…

    // Todo: This is a terrible name.
    // Figure out what makes more sense in your domain, by seeing where you use it
    public class VisibleTags
    {
        private readonly IMyContextClass context;
    
        public VisibleTags(IMyContextClass context)
        {
            this.context = context;
        }
    
        // Todo: Try to see if you can get this to return IQueryable.
        // I haven't used Union, so I'm not sure if it breaks that ability or not...
        public IEnumerable<Tag> GetVisibleTags(User user)
        {
            return context.Tags
                .Where(t => t.PrivateUserID == null)
                .Union(user.Tags)
                ;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.