save a new collection with relations

Posts   
 
    
c_pousson
User
Posts: 12
Joined: 03-Jul-2008
# Posted on: 28-Jul-2008 17:38:02   

I have an entitycollection called of type person

an example of an item: name: Chris employer:<employerEntity>

is there a way to do this:

I want to add a new person item to the collection and in doing so, I would like the employer entity to not be null so I could add values to it and then save the collection recursively so as to create a new row in employer also.


person.Add(new PersonEntity)
//instantiate the employerEntity inside new personentity
person.employer.name = "some company";
saveEntityCollection(person,true,true);

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Jul-2008 07:23:54   

Sorry, I don't understand your code. I think it wont work. Shouldn't be:

// given people, a PersonCollection, add a new person
PersonEntity newPerson = people.AddNew();

// assign the proper person's fields
...

// create the employer
person.Employer = new EmployerEntity();
person.Employer.Name = "some company";
// some other employer's fields
...

// save changes
saveEntityCollection(people, true, true);

Is that what you are looking for?

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39866
Joined: 17-Aug-2003
# Posted on: 30-Jul-2008 10:22:32   

Person is a collection ? If so, it doesn't have a property of Employer, the individual entities do. Am I correct?

Frans Bouma | Lead developer LLBLGen Pro
c_pousson
User
Posts: 12
Joined: 03-Jul-2008
# Posted on: 30-Jul-2008 16:49:45   

I guess I need to clarify.

this is not at all like our database but will do for this example:

--table person-- PersonID PersonName FKEmployerID

--table employer-- EmployerID EmployerName

ok so I fetch an entity collection of type person and it returns 4 rows so


personCollection.Items.Count ==4

now I have this databound to a grid I created. when a new row is added to the grid, a new item is added to the personCollection

the problem comes in when I want to save the new row into the database. Honestly, Im not sure that llblgen was meant to accomodate what Im trying to do or not.

what I want to do is this:


bool recurse = true;

personCollection.Items.Add(new PersonEntity());
personCollection.Items[newRow].Employer.EmployerName = "some grid cell value";
DataAdapter.SaveEntityCollection(personCollection,recurse);

the problem is, that when I add a new item to the collection the related Employer entity inside of person is null and I can't instantiate it. Is there someway around this. If not, I can do it differently but I liked the feel of this.

thanks again.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Jul-2008 17:18:11   

Well, in the UI how would you decide about the employer? Also is this a Web or Windows application?

There are many events available to you (whether UI events or LLBLGen Pro events, like collection.EntityAdded) where you can inject the employer.

c_pousson
User
Posts: 12
Joined: 03-Jul-2008
# Posted on: 30-Jul-2008 17:27:59   

this is a windows app.

the thing about it is that this is a new entity in the collection so there is no rows in the database for either person or employer of the person. So Im trying to add both new rows recursively. I could always just make a new entity of type employer but I was just curious if there was some way to access a new entity of the related employer in a new person entity with no values.

c_pousson
User
Posts: 12
Joined: 03-Jul-2008
# Posted on: 30-Jul-2008 17:29:44   

oops, marked the wrong thread as done...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39866
Joined: 17-Aug-2003
# Posted on: 30-Jul-2008 17:33:38   

c_pousson wrote:

oops, marked the wrong thread as done...

Which thread was that? This thread? It's now undone wink

Frans Bouma | Lead developer LLBLGen Pro