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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:44:30+00:00 2026-05-11T21:44:30+00:00

I have a relational model with an associative table. But in addition to the

  • 0

I have a relational model with an associative table. But in addition to the related keys, this table also has a flag. I would like to define two associations: one where the flag is true and another where it is false. The EF designer can add a condition to an entity, but not to an association.

The associative table looks like this:

UserPrivilege
-------------
UserId int (FK1)
PrivilegeId int (FK2)
IsGranted bit

I would like to create two associations between the User entity and the Privilege entity: PrivilegesGranted and PrivilegesDenied.

  • 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-11T21:44:31+00:00Added an answer on May 11, 2026 at 9:44 pm

    You can’t do this directly through the designer.

    But this is possible in the XML using DefiningQuery and Create and Delete sprocs. See this old post on my blog for more: Associations with Payloads.

    The only thing that is a bit interesting is I assume the PK is just UserId and PrivilegeId, which means a user can’t be granted and denied for a particular privilege at the same time.

    So if you write code like this:

    Privilege p = user.Granted.First();
    user.Granted.Remove(p);
    user.Denied.Add(p);
    ctx.SaveChanges();
    

    Update ordering is important. because you are using a DefiningQuery for both associations, the EF doesn’t know they are related, and that it needs to do the delete before it can do the update.

    So you might end up with PK violations.

    A way to address this issue is in the Insert and Delete sprocs for each association, you can essentially make them change the current row for the User and Privilege pair, if it exists update it with the correct IsGranted value, if not create it, i.e. make it an upsert.

    Let me know how you go here

    Alex

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

Sidebar

Related Questions

Hi have a table named category which has two foreign keys to the table
I'm stumped on this. I have a simple backbone-relational model: window.Site = Backbone.RelationalModel.extend({ idAttribute:
I'm working on this requirement: I have an object-relational model quite big, where entities
I have a relational model where users have managers that are also users. The
I have a current City and Country model like this # City model city:string
I have a simple SQL relational model with a many to many relation. Here
RDBMS are based on Relational Algebra as well as Codd's Model. Do we have
I have a Profile model and two ActiveRecord::Relations of this model. SQL request of
I have a model Client which has many :tours, association in it. I am
I have a relationship in a Core Data model that feels like it wants

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.