Number of related records before delete entity

Posts   
 
    
Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 13-May-2007 13:26:58   

Hi,

I'm using the SelfServicing model with v2.

Can you tell me if there's a simple way to find the number of related records before I delete an entity.

The Entities are related via a linking table, which only contains the PKs of the two related tables ie, users and roles linked by usersInRoles.

I want to delete a role from the roles table but first check whether there are users with that role.

Thanks

PS. It's using a SqlServer 2000 database.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-May-2007 21:06:08   

I would use something like this:

// select count(userId) from usersInRoles where roleId = ???
UsersInRolesCollection usersInRoles= new UsersInRolesCollection();
object res = usersInRoles.GetScalar(UsersInRolesFieldIndex.UserId, null, AggregateFunction.Count, UsersInRolesFields.RoleId == yourRoleEntity.RoleId);

int numberOfRelatedUsersToThisRole = (res == null) ? 0 : (int)res;
David Elizondo | LLBLGen Support Team