Beginner Question - Adding an new entity and assigning its properties

Posts   
 
    
AndyMcG
User
Posts: 3
Joined: 22-Apr-2005
# Posted on: 22-Apr-2005 15:12:15   

Hi, I was looking for some help with the following situation.

I have 3 tables User, UserGroup, and Group. User contains user information, Group contains group information and UserGroup is a mapping to allow a many to many relationship between User and Group.

I am encountering a problem when I create a new User and then try and add some groups to the user.

I create the UserEntity object and then assign some values to its properties, which all goes according to plan.

When I try and add 1 or more groups as selected from a ListBox I get an

        // UserEntity _user = new UserEntity() - define earlier in code
        foreach (GroupEntity aGroup in groupsList.SelectedItems)
        {
            Console.WriteLine(aGroup.Name.ToString());              
            _user.Group.Add(aGroup);

        }

At the line [b]"_user.Group.Add(aGroup);[/b]" I get the following error **"An unhandled exception of type 'System.InvalidOperationException' occurred in sd.llblgen.pro.ormsupportclasses.net11.dll

Additional information: This collection is read-only."**

I am probably attacking this problem wrongly so any advice would be appreciated

Thanks

Andy

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 22-Apr-2005 16:55:23   

Hi,

Try creating, inside the loop, new UserGroup entities and assign the new UserEntity and GroupEntity to each.

AndyMcG
User
Posts: 3
Joined: 22-Apr-2005
# Posted on: 22-Apr-2005 17:18:30   

Thanks for that, never thought of that. simple_smile

I can see how this will work, but won't this mean having to make an

    adapter.SaveEntity(myUserGroup);

call as well as a call to save the UserEntity.

My problem is that I wanted all changes to the user to be made to the UserEntity so that it could be passed back to the caller where all true database access is made.

Maybe its not possible though.

On further investigation I have found that the generated code for the Group property of the UserEntity only has a getter and no setter. This is obvioulsy the root cause for my problem, but is this correct?

Thanks again.

Andy

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 22-Apr-2005 18:51:34   

AndyMcG wrote:

Thanks for that, never thought of that. simple_smile

I can see how this will work, but won't this mean having to make an

    adapter.SaveEntity(myUserGroup);

call as well as a call to save the UserEntity.

My problem is that I wanted all changes to the user to be made to the UserEntity so that it could be passed back to the caller where all true database access is made.

Maybe its not possible though.

On further investigation I have found that the generated code for the Group property of the UserEntity only has a getter and no setter. This is obvioulsy the root cause for my problem, but is this correct?

Thanks again.

Andy

This is expected behavior. As the relationship between User and Group is n:m, the "mapper" table, "UserGroup", must have a record inserted for each new Group related to User. Unfortunately, "UserGroup" could contain other columns making it an "objectified relation". The framework has no way of knowing how to populate those columns.

The solution is as above: create a UserGroup and a Group entity; set UserGroup.Group = to your new Group entity, then add the UserGroup to User via User.UserGroup.Add(your UserGroup).

Once all of the required entities are loaded into your User entity, passing the User entity to adapter.SaveEntity() will save the User entity and recursively save all entities in the graph, including the newly created UserGroup and Group entities.

Jeff...

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 22-Apr-2005 20:49:00   

AndyMcG wrote:

Thanks for that, never thought of that. simple_smile

I can see how this will work, but won't this mean having to make an

    adapter.SaveEntity(myUserGroup);

call as well as a call to save the UserEntity.

My problem is that I wanted all changes to the user to be made to the UserEntity so that it could be passed back to the caller where all true database access is made.

You need only adapter.SaveEntity(UserEntity), that will save all the new myUserGroup that you haved created and assigned the UserEntity to them.

AndyMcG
User
Posts: 3
Joined: 22-Apr-2005
# Posted on: 23-Apr-2005 01:35:10   

Got you now! :-)

The only thing I can see to prevent this working is that in the generated code the UserGroup property of the User class is defined as:

[TypeContainedAttribute(typeof(UserGroupEntity))] public virtual EntityCollection UserGroup { get { if(_userGroup==null) { // create on demand. _userGroup = new EntityCollection(new UserGroupEntityFactory());

        _userGroup.SetContainingEntityInfo(this, "User");

    }
    return _userGroup;
}

}

This looks like it will not allow me to set the UserGroup property.

It could be that I have made an error in the entity defination in the dataabase that has caused this.

I havent yet had the chance to try assigning this property a value, will give that a go and see where that gets me.

Thanks again!

Andy

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 23-Apr-2005 04:06:56   

AndyMcG wrote:

Got you now! :-)

The only thing I can see to prevent this working is that in the generated code the UserGroup property of the User class is defined as:

[TypeContainedAttribute(typeof(UserGroupEntity))] public virtual EntityCollection UserGroup { get { if(_userGroup==null) { // create on demand. _userGroup = new EntityCollection(new UserGroupEntityFactory());

        _userGroup.SetContainingEntityInfo(this, "User");

    }
    return _userGroup;
}

}

This looks like it will not allow me to set the UserGroup property.

It could be that I have made an error in the entity defination in the dataabase that has caused this.

I havent yet had the chance to try assigning this property a value, will give that a go and see where that gets me.

Thanks again!

Andy

Andy,

You can add the new UserGroup to the User.UserGroup collection or you can assign the UserEntity to the User property of your UserGroup entity.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 23-Apr-2005 11:34:03   

AndyMcG wrote:

Got you now! :-)

The only thing I can see to prevent this working is that in the generated code the UserGroup property of the User class is defined as:

[TypeContainedAttribute(typeof(UserGroupEntity))] public virtual EntityCollection UserGroup { get { if(_userGroup==null) { // create on demand. _userGroup = new EntityCollection(new UserGroupEntityFactory());

        _userGroup.SetContainingEntityInfo(this, "User");

    }
    return _userGroup;
}

}

This looks like it will not allow me to set the UserGroup property.

Correct, it returns the collection, you then add elements to that collection: user.UserGroup.Add(myUserGroup); simple_smile

It could be that I have made an error in the entity defination in the dataabase that has caused this. I havent yet had the chance to try assigning this property a value, will give that a go and see where that gets me.

Rogelio and Jeff have given you the answer to this simple_smile The thing is that m:n relations aren't hiding the intermediate entity in LLBLGen Pro, if you want to construct the relation between 2 entities, you have to create the intermediate entity as well.

Frans Bouma | Lead developer LLBLGen Pro