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

  • Home
  • SEARCH
  • 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 561737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:28:52+00:00 2026-05-13T12:28:52+00:00

I’m new to Entity Framework and am trying to figure things out. I have

  • 0

I’m new to Entity Framework and am trying to figure things out. I have a database created that’s not very complicated. There’s about 7 tables and 3 of those are mapping tables to associate one table record w/ another. The example I’m using here is this:

  • Table User

    • UserId
    • UserName
  • Table Role

    • RoleId
    • RoleName
  • Table: UserRole

    • UserId
    • RoleId

The foreign keys are mapped in my database. When I create a new Entity Model inside of VS 2008, the diagram seems to have the relationships correct, but does not create a table for the UserRole table. The relationship is mapped as a Many to Many between User & Role.

My problem is that I can create new users and I can create new roles, but I cannot figure out how to create a new user with an existing role. Furthermore, the UserRole table is probably not mapped correctly within the model to begin with. Here’s my code so far:

ObjectQuery<Role> roles = context.Roles;
Role role = context.Roles.Where(c => c.RoleName == "Subscriber").First();

 User user = new User
 { 
  DisplayName = "TestCreate2",
  Email = "test@test.com",
  Password = "test"
 };            
 context.AttachTo("Roles", role);
 user.Roles.Add(role);            
 context.AddToUsers(user);
 context.SaveChanges();

Here’s the error I am getting:

Unable to update the EntitySet ‘UserRoles’ because it has a DefiningQuery and no element exists in the element to support the current operation.

Here is the xml that pertains to the UserRole table:

<EntitySet Name="UserRoles" EntityType="RememberTheJourneyModel.Store.UserRoles" store:Type="Tables" store:Schema="dbo" store:Name="UserRoles">
        <DefiningQuery>SELECT 
  [UserRoles].[Role_id] AS [Role_id], 
  [UserRoles].[User_id] AS [User_id]
  FROM [dbo].[UserRoles] AS [UserRoles]</DefiningQuery>
      </EntitySet>

It was pulling teeth just to figure out how to query the context such that it gave me an actual Role entity. I’m sure the issue is something to do with how UserRole is mapped, but I’m just getting started here and haven’t any idea where that might have gone wrong.

And I really have searched google & this site, but I suppose I haven’t come up with the right search parameters to find a question that helps me fix this. I found one question that said EF has issues with this, but if you’re mapping table makes both columns a primary key, it works itself out. I’m not sure how to do that. Is this done in the database (using SQL SERVER 2005 EXPRESS) or in the mapping? This is a personal project so I can post more details about the code or xml if needed. Any and all help will be appreciated.

  • 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-13T12:28:53+00:00Added an answer on May 13, 2026 at 12:28 pm

    I remember running into this issue before as well… and I believe I fixed it by making both columns keys in my association table.

    You can make both columns the primary key in SQL Server Management Studio table designer.
    Just highlight both the User_Id and Role_Id at the same time, right click on the left and select Primary Key… this will make it so that no record can have the same combination of User_Id and Role_Id.. making each entry unique.

    Then you can update your data model in the Entity Framework designer.. and hopefully be good to go.

    I believe that if a table does not have any keys defined to ensure records are unique, then EF would not be able to update records properly, and so the framework guards against that case.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a jquery bug and I've been looking for hours now, I can't

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.