none
EntityFramework Update problem with Many To Many Relationship RRS feed

  • Question

  • Hi, I have this Many To Many Relationship in my DbContext

     modelBuilder.Entity<Role>().HasMany(r => r.Permissions).WithMany(p => p.Roles)
                    .Map(
                        m =>
                        {
                            m.MapLeftKey("role_id");
                            m.MapRightKey("per_id");
                            m.ToTable("roles_permissions");
                        }
                    );

    The problem is that when I try to update a Many To Many Table displays this error, in this line:

     db.SaveChanges();
    

    An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

    Here is the code I use to try to update:

    private void btn_rol_modificar_Click(object senderEventArgs e)
            {
                using (var db = new MySqlContext())
                {
    
                    //Get the specific Role
                    Role role = (from s in db.Roles
                                 where s.Name == txt_modificar_nombre.Text
                                 select s).FirstOrDefault<Role>();
     
                    //Get the permissions for that specific role
                    var permissions = db.Roles.Where(r => r.Name == txt_modificar_nombre.Text)
                                      .SelectMany(r => r.Permissions);
     
     
                    foreach (var item in checkedListBox_modificar_permissions.CheckedItems)
                    {
                        //get the Checked items (permission names) 
                        Permission newPermission = (from r in db.Permissions
                                                     .Where(n => n.Name == item)
                                                    select r).FirstOrDefault<Permission>();
                        //Add the new permission to the specific role
                        role.Permissions.Add(newPermission);
                        db.SaveChanges(); //HERE IS THE  exception throws
     
                    }
     
                   
                    MessageBox.Show("!");
     
                    
                }
            }

    Hope you guys can help me

    Saturday, February 1, 2014 9:33 PM

Answers

  • The code you have posted looks is fine in theory. Except maybe it would be better to move the call to SaveChanges to after the foreach loop has completed. Have a look at the inner exception to get the exact error. I think it will be a PK violation though as at the moment you are not checking to see if a relationship already exists so the code will try to add duplicate records.
    Sunday, February 2, 2014 9:39 AM