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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:35:19+00:00 2026-05-11T19:35:19+00:00

If I a many-to-many relationship between Users and Roles and I have an instance

  • 0

If I a many-to-many relationship between Users and Roles and I have an instance of a User entity and several Role Ids can I insert a relationship between the two types of entities without having any other Role data and without doing a select on the Roles first?

Update:

I might not have been clear enough. I don’t have an instance of a Role, only the role id. Is it possible to create the relationship between User and Role without filling a Role object from the database first?

  • 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-11T19:35:19+00:00Added an answer on May 11, 2026 at 7:35 pm

    Yes if you have the IDs and you need to relate them

    You should be able to do this (pseudo code)

    // how you get this doesn't matter so long as it is in the Context
    User user = ...; 
    Role role = new Role {Id = 2}; 
    // role 2 is in unchanged state
    ctx.AttachTo("Roles", role); 
    // role 2 is unchanged + added relationship between user and role 2
    user.Roles.Add(role); 
    ctx.SaveChanges(); 
    

    The key here is that AttachTo puts an entity into the ObjectState manager in the unchanged state. So long as you don’t need to modify that entity, and only use if for relationship building, you don’t even need to know all the property values, the PK is sufficient.

    Once you have it attached you can then build the relationship.

    Hope this helps

    Cheers
    Alex

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

Sidebar

Related Questions

Let's say I have a many-to-many relationship between users and group. A user can
I have a many-to-many relationship between two models, Lists and Users. When a user
I have a standard many-to-many relationship between users and roles in my Rails app:
I have two tables, users and groups . A user can belong to many
I have two tables: messages (messages users posted), likes (many to many relationship between
I have a many-to-many relationship between User and Task model. A task can have
I have a many-to-many relationship between users and groups and I have a table
I have a many-to-many relationship between User s and Task s. I want the
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a one-to-many relationship between two classes for this situation. I have a

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.