recursive save 1:1

Posts   
 
    
Posts: 67
Joined: 10-Jun-2009
# Posted on: 20-Oct-2009 14:15:51   

I have two entities: PersonEntity and CredentialsEntity which have a 1:1 relation between them.

When I want to create a new person with credentials, this is my code:


PersonEntity person = new PersonEntity();
person.Credentials = new CredentialsEntity();

person.Save(true);

When I reach the last line, a sqlexception is thrown because the referencing column in the credentials table, personId, can't be null.

How can I recursively add and save records with a 1:1 relation? I could first save the person and after that save the credentials with the proper personId, but I think it should be done using a recursive save.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 20-Oct-2009 21:01:50   

Does


PersonEntity person = new PersonEntity();
CredentialsEntity cred = new CredentialsEntity();
cred.Person = person;

person.Save(true);

work ?

Matt

Posts: 67
Joined: 10-Jun-2009
# Posted on: 21-Oct-2009 09:50:03   

It seems a bit odd to set a property of a child object in order for this to work. However, it works. Thanxs for your reply.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 21-Oct-2009 10:02:32   
PersonEntity person = new PersonEntity();
CredentialsEntity cred = new CredentialsEntity();

person.Credentials = cred;
person.Save(true);

This also should work.

Posts: 67
Joined: 10-Jun-2009
# Posted on: 21-Oct-2009 10:22:47   

I like that option better, thanxs.