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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:11:13+00:00 2026-05-23T07:11:13+00:00

I have the following tables/views Users (View) -> UserId -> Name Roles (Table) ->

  • 0

I have the following tables/views

Users (View)
-> UserId
-> Name

Roles (Table)
-> RoleId
-> Name

UserRoles (Table)
-> UserId
-> RoleId

and the classes

public class Role{
     public int RoleId{get;set}
     public string Name{get;set}
}

public class User{
     public int UserId{get;set}
     public string Name{get;set}
     public ICollection<Role> Roles{get;set}
}

and the save method

using (var helper = new DbContext())
{
     helper.Users.Attach(user);
     helper.SaveChanges();
}

As you can see above, the Users is a view and UserRoles is a mapping table. I am able to retrieve User entities along with the mapped Roles. But while saving it is not throwing any exceptions nor is it saving. I tried checking the db using profiler and it is not even hitting the db.

Since Users is a view I don’t want to save the User entity but only the changes made in the Roles collection.

  • 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-23T07:11:13+00:00Added an answer on May 23, 2026 at 7:11 am

    This cannot save anything. Attach puts the whole object graph into the context, but in state Unchanged. If all objects in the context are unchanged SaveChanges won’t issue any command to the DB (because for EF nothing has changed).

    If you want to make changes which EF recognizes as such you must first attach the object which represents the state in the Db and then make your changes, something like:

    using (var helper = new DbContext())
    {
        helper.Users.Attach(user);
        helper.Roles.Attach(myNewRole);
        user.Name = myNewName;
        user.Roles.Add(myNewRole);
        // etc.
        helper.SaveChanges();
    }
    

    Alternatively you can mark the user as modified:

    helper.Entry(user).State = EntityState.Modified;
    

    But I believe this only affects scalar properties of the entity and it doesn’t solve the problem to add a new role to the user.

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

Sidebar

Related Questions

I have the following tables: users - userid - real name - other stuff
My Schema I have the following tables table notes/example values ------------------------------------------------ users ( id
I have following view <asp:Content ID=Content2 ContentPlaceHolderID=MainContent runat=server> <h2>Tables <%=ViewData[RetriverName] %></h2> <%using (Html.BeginForm(ResfreshSelectedTables, Home))
I have the following tables in MySQL: team: id, name, [more stuff] person: id,
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
I have the following tables CREATE TABLE `files` ( `fileid` int(11) NOT NULL AUTO_INCREMENT,
I have two tables Users and Expenses in the backend. UserId is a foreignKey
I have the following tables - groups, contacts, contacts_groups (habtm join table) groups &
I have the following problem while i scroll the table view: NSCFString objectAtIndex:]: unrecognized
I want to find the users(userid) from a permissions table who have all of

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.