locked
Entities in participate in the 0 related 'Role' were found. 1 'Role' is expected. RRS feed

  • Question

  •  
    I have 3 tables

    Roles
    ------
    RoleId
    RoleName
    RoleDescription
    CustomPermissions
    -----------------
    PermissionId
    PermissionName
    PermissionDescription
    RolePermissionMappings
    ------------------------
    RoleId (PK)
    PermissionId(PK)
    Role_RoleId(FK)
    CustomPermission_PermissionId(FK)

    Idividually i am able to add values to Roles and CustomPermissions table.
    While i am trying to map the values in RolePermissionMappings table i am getting this error.

    Role has n number of permissions, they have to map as like this

    RoleId   PermissionId
    ------   ------------
      2          2
      2          4
      2          5
      2          8
      2          10

    my code here

    bool ICustomPermissions.AddPermissionsToRole(RoleDataContract role, List<PermissionDataContract> permissions)
            {
               using(_context = new RolePermissionMappingContainer())
               {
                   RolePermissionMapping rpm = new RolePermissionMapping();
                   {
                       rpm.RoleId =  role.RoleId;
                       
                       foreach (PermissionDataContract pdc in permissions)
                       {
                           rpm.PermissionId =  pdc.PermissionId;
                           //_context.RolePermissionMappings.AddObject(rpm);
                           _context.RolePermissionMappings.AddObject(rpm);
                           _context.SaveChanges();
                       }
                   }
               }
               return true;
            }

    Please correct me where im getting in to the error.

    Monday, June 18, 2012 3:01 PM

Answers

  • Hi SPBee,

    Welcome to MSDN Forum.

    Please refer to the code below, it works well.

     class Program
        {
            static void Main(string[] args)
            {
                using (TestEntities context = new TestEntities())
                {
                    Role role = context.Roles.First();
                    List<Permission> permissionList = context.Permissions.ToList();
                    MappingTable mt;
                    for (int i = 0; i < permissionList.Count; i++)
                    {
                        mt = new MappingTable();                    
                        mt.Role_RoleId = role.RoleId;
                        mt.RoleId = role.RoleId;
                        mt.CustomPermission_PermissionId = permissionList[i].PermissionId;
                        mt.PermissionId = permissionList[i].PermissionId;
                        context.MappingTables.AddObject(mt);
                        context.SaveChanges();
                    }
                }
            }
        }

    I queried out all the permissions out and assigned to one role, you can add where statement to filter the permissions.

    BTW, based on this issue, the design isn't sensible, 'Role' and 'Permission' are many to many relationship in fact, we needn't to explicitliy create a mapping table for these two tables, just creating a many to many relationship to these two tables is Ok. Please look at the code above, we have to assign 'RoleId' and 'PermissionId' twice, but if we only create a relationship between these two tables, we can only assign the role entitites or appropriate permission entities to the other's navigation property is Ok.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Doraemon_3 Tuesday, June 19, 2012 6:17 AM
    • Marked as answer by SPBee Tuesday, June 19, 2012 2:53 PM
    Tuesday, June 19, 2012 3:14 AM

All replies

  • Please state the error information.

    Thanks.

    Monday, June 18, 2012 3:24 PM
  • Hi SPBee,

    Welcome to MSDN Forum.

    Please refer to the code below, it works well.

     class Program
        {
            static void Main(string[] args)
            {
                using (TestEntities context = new TestEntities())
                {
                    Role role = context.Roles.First();
                    List<Permission> permissionList = context.Permissions.ToList();
                    MappingTable mt;
                    for (int i = 0; i < permissionList.Count; i++)
                    {
                        mt = new MappingTable();                    
                        mt.Role_RoleId = role.RoleId;
                        mt.RoleId = role.RoleId;
                        mt.CustomPermission_PermissionId = permissionList[i].PermissionId;
                        mt.PermissionId = permissionList[i].PermissionId;
                        context.MappingTables.AddObject(mt);
                        context.SaveChanges();
                    }
                }
            }
        }

    I queried out all the permissions out and assigned to one role, you can add where statement to filter the permissions.

    BTW, based on this issue, the design isn't sensible, 'Role' and 'Permission' are many to many relationship in fact, we needn't to explicitliy create a mapping table for these two tables, just creating a many to many relationship to these two tables is Ok. Please look at the code above, we have to assign 'RoleId' and 'PermissionId' twice, but if we only create a relationship between these two tables, we can only assign the role entitites or appropriate permission entities to the other's navigation property is Ok.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Doraemon_3 Tuesday, June 19, 2012 6:17 AM
    • Marked as answer by SPBee Tuesday, June 19, 2012 2:53 PM
    Tuesday, June 19, 2012 3:14 AM
  • Tin,

    This is the error

    Entities in 'RolePermissionMappingContainer.RolePermissionMappings'
    participate in the 'RoleRolePermissionMapping' relationship. 0 related 'Role' were found. 1
    'Role' is expected.

    Tuesday, June 19, 2012 2:56 PM
  • I have a one to many relationship table structure. I have a parent table and I need to enter values in child table with a relationship to parent.

    I used the below code but I'm getting error "0 related enties" found for foreign key relationship

    var tbl_row = context.tbl_Parent.Where(r=>r.ID==6).Single();                
                    var tbl_ChildObj= new context.tbl_Child
                    {
                        ConsentText = "Some Consent Text",
                        ConsentType = "SEL",
                        CreatedDate = DateTime.Now,
                        UserId = "SK Saha"                  
                    };
                    
                   tbl_row.tbl_Childs.Add( tbl_ChildObj);
                    context.AddTotbl_Child( tbl_ChildObj);
                    clientproxy.SaveChanges();

    can you please help me understand where I'm failing.


    Thanks & Regards, Soumyadev | Posting is provided "AS IS" with no warranties, and confers no rights.

    Sunday, March 2, 2014 6:54 AM