locked
Update Entity with One to many relationship RRS feed

  • Question

  • I am running into problem where there is One to may relationship and i am trying to update the data in user table
    I have two table User and Roles and one Link/Mapping Table UserRoles
     containing mappiong of User with roles and user table has 1(User) - many relationship(Role)
    However importantly the Roles are predifined and data would always be 3 rows as shown below. So
    i want to update the User with Roles. I am unable to do with the following code.
    If someone can help how should i acheive this.

    For e.g Orginal :
    User : John
    Roles : Admin, User

    Update to
    User : John
    Roles : User

     

    Table- User
    UserID : Pk
    Name


    Table- Roles (Data is not added in this table, Data already exists in this table)
    RoleID : Pk
    Name

    Predined Data
    RoleID   Name
    1  Admin
    2  User
    3  None

     

    Table- UserRoles
    UserID  : PK
    RoleID : Pk

    Corresponding to this entity are like this

    Entity-User
    UserId
    Name
    EntityCollection<Role> Roles


    Entity-Role
    RoleId
    Name
    EntityCollection<User> Users

    I want to Update/Edit a User. But have not figured a way to do this

    i have tried with following code

    public void update(int userId, string newusername, list<int> roleList)
    {
    using (DBEntites context = new DBEntites())
                {
                    
                    User objUserInDb = new User();
      objUserInDb.UserID = userId;
      Context.Users.Attach(objUserInDb);
      objUserInDb.Name = newusername;
    
      objUserInDb.Roles.Clear(); 
      //
      // TRIED TO USE The remoe method objUserInDb.Roles.Remove, but it is returning false
      //      i have enumerated the remove method on objUserInDb.Roles.Select(x=>x.RoleID).ToList()
      // 
      
      
      
      foreach (long pkIDToAdd in roleList)
                    {
                        Role objRoleInDb = new Role();
          objRoleInDb.RoleId = pkIDToAdd;
    
       //
          // GIVES EXCEPTION 
          //An object with the same key already exists in the ObjectStateManager. 
          //The ObjectStateManager cannot track multiple objects with the same key. 
         //
                        context.RiskTypes.Attach(objRoleInDb);
    
                        objUserInDb.Roles.Add(objRoleInDb);
                    }
    
      }
    
                    
    }
    

     

     

     

     


    Abhinav Galodha
    • Edited by Agalo Thursday, October 20, 2011 4:39 PM
    Thursday, October 20, 2011 4:39 PM

Answers

  • Hi Abhinav,

    it looks like you have a many-to-many relationship, not one-to-many.

    Also, are you updating existing users, or adding new ones?

    In the following example I am adding a new person and associating it with an existing course that has ID 1050.

    using (var context = new SchoolEntities())
    {
        Person person = new Person();
        person.PersonID = 55;
        person.FirstName = "Julia";
        person.LastName = "Kornich";
        person.Courses.Add(context.Courses.Where(c=>c.CourseID==1050).FirstOrDefault());
        context.SaveChanges();
    }

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, October 21, 2011 8:30 PM