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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:34:26+00:00 2026-06-04T02:34:26+00:00

I am trying to expose the linked class that EF code first automatically creates

  • 0

I am trying to expose the linked class that EF code first automatically creates in many to many relationships as a separate object because that linked object needs to be referenced in other classes, however I seem to have a problem with getting the data that exists in the database.

I’ve got the following 3 objects:

 public class Role : Entity
{
    public virtual ICollection<User> Users { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Permission> Permissions { get; set; }
}

public class User: Entity
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public virtual ICollection<Role> Roles { get; set; }
 }

 public class UserRole : Entity
{
    public User User { get; set; }
    public Role Role { get; set; }
}

This creates the following tables:

enter image description here

Now I can see the problem is that it is creating a RoleUsers table, when it shouldn’t and should just use my UserRoles table. How can I force the linked table to be UserRoles so that I can have that linked object exposed in EF so I can use it in other objects?

Also, how would I be traversing the objects then when writing queries? Would I still be able to use User.Roles.Any(y => y.Name == “blah”), or would I now have to query by going User.UserRoles.Any(y => y.Role.Name == “blah”)? I suppose it isn’t possible to maintain the transparent linking while simultaneously exposing the linked object?

Edit: One of the classes that references UserRole as a navigation property looks like this:

 public class UserRoleEntity : Entity
{
    public UserRole UserRole { get; set; }
    public Guid EntityId { get; set; }
    public EntityType EntityType { get; set; }
}

I don’t want to individually store the User and Role object in this class because then they are not tightly coupled and data could be orphaned.

  • 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-04T02:34:27+00:00Added an answer on June 4, 2026 at 2:34 am

    You don’t need to explicitly create UserRole entity in your code. since you have the navigational property of Roles in Users and Users in Roles, EF will automatically create a relationship between them.

    EDIT : If you want to create the link table and use that as a property of another class, you can do it like this.

    public class User 
    {
        public int UserID { set; get; }
        public string FirstName { get; set; }     
        public string LastName { get; set; }
        public virtual ICollection<UserRole> UserRoles { get; set; }      
    }
    public class Role 
    {      
        public int RoleID { set;get;}
        public string Name { get; set; }
        public string Description { get; set; }
        public virtual ICollection<UserRole> UserRoles { get; set; }
    }
    public class UserRole 
    {
        public int UserRoleID { set; get; }
        public virtual User User { get; set; }
        public virtual Role Role { get; set; }
    }
    public class AnotherEntity
    {
        public int ID { set; get; }
        public int UserRoleID { set; get; }     
    }
    

    and you will have your tables created like this

    enter image description here

    And you are correct, You have to access it like this

     StringBuilder stRoleNames = new StringBuilder();
     var user1 = dbContext.Users.Where(x => x.UserID == 34).SingleOrDefault();
     var userRoles = user1.UserRoles;
     foreach (var userRole in userRoles)
     {
         stRoleNames.Append(userRole.Role.Name);
     } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to expose a model class via WCF Data Services that has an
I'm trying to write a web app that needs to expose some methods that
Trying to get comfortable with jQuery and I have encountered some sample code that
I'm trying to expose a fairly simple C# class to COM which should be
I am trying to expose a single well defined class by building a static
I am trying to expose some Java web services so that I can interoperate
I'm trying to expose some events from a private object which is contained inside
I´m trying to expose services using jax-ws but the first surprise i got was
I'm trying to expose the attachment functionality in my c# code from MapiEx.dll using
I'm trying to expose an API such that, I do the following RegisterCallback<T>(Action<T> func)

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.