Can one add/remove entities from a collection using the m:n relationship?

Posts   
 
    
Greps
User
Posts: 1
Joined: 28-Jul-2009
# Posted on: 28-Jul-2009 13:57:04   

This question may sound a bit vague, so let me clarify. Say I have a Group entity which contains Users. Any user can be in multiple groups. Now if I want to add a user (to the group) I could do that this way:


GroupUserEntity gu = new GroupUserEntity();
gu.UserId = user.UserId;
gu.GroupId = group.GroupId;
gu.Save();

What I would like to know is, is if it's also possible to do this like this (I tried it and I can't seem to get it working):


group.Users.Add(new UserEntity(user.UserId));
group.Users.SaveMulti(true);

Also I would like to know if it's possible to delete in this way, like this:


group.Users.Delete(new UserEntity(user.UserId));

I would like this to remove the relationship, not the actual user.

I have hidden the relationships with the linktable in the group and user entities because I think LLBLGen abstracts this away nicely, but maybe this isn't the brightest idea.

Am I on the good path here, or do I just need to stop whining and use the linktable entities?

(using LLBLGen 2.6 latest)

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 28-Jul-2009 15:22:25   

The way I have handled this in the past is to add .AddUser or .RemoveUser methods to the group class (either in partial classes, user code regions, or as extension methods)

These can then handle the modification of both the user and m:m UserGroup collections.

Matt