Problem with n:m relation How add data to concurrent table?

Posts   
 
    
kalvin
User
Posts: 2
Joined: 26-Apr-2009
# Posted on: 26-Apr-2009 19:49:29   

Hi, This is link for picture img5.imageshack.us/img5/8507/41657828.jpg witch shows example tables in my database. I have problem with add many data to muttual table "DepartmentUser". What i want to do? I have a lot solid data in table "Department" and i want to add new "User" and in the same time add him with many PK-Department to "DepartmentUser" - i know how to add only one row(one department - one user) to "DepartmentUser but, I don't know how to add one user and many depeartment to table "DepartmentUser"


//Create User
UserEntity entity = new UserEntity();
            entity.IdUser = 1;
            entity.Name = "Some name";
//Create DepartmentUser and add User and connect him with Department.
//I want to add collection od Department
            DepartmentUserEntity test = new DepartmentUserEntity();
            test .User = entity;
            test .IdDepertment = 2;
            test .User.Save();
            test .Save();

Thank in advance

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Apr-2009 23:03:55   

The UserEntity have a 1:n relation to DepartamentUser, that means that exists a DepartamentUserCollection on your user objects. That means that you could do something like this:

//Create User
UserEntity entity = new UserEntity();
entity.IdUser = 1;
entity.Name = "Some name";

// add one departamentUser relation
DepartmentUserEntity test = new DepartmentUserEntity();
test.User = entity;
test.IdDepertment = 2;
entity.DepartmentUser.Add( test );

// add another departamentUser relation
DepartmentUserEntity test2 = new DepartmentUserEntity();
test2.User = entity;
test2.IdDepertment = 2;
entity.DepartmentUser.Add( test2 );

// save recursively all graph
entity.Save(true);
David Elizondo | LLBLGen Support Team
kalvin
User
Posts: 2
Joined: 26-Apr-2009
# Posted on: 26-Apr-2009 23:19:55   

Thanks for answer, All works finesimple_smile