views:

16

answers:

1

Hi everybody,

I'm trying to add an object:

Person (id, firstName, lastName) 

and another object :

Details(id, personId, phoneNumber, address).

Of course, the "id" column in Person and Details is auto incremented. And the "personId" foreign Key is mandatory in Details.

I'm using Entity Framework. I created some basic functions that allow me adding and removing objects:

Person p = new Person();
p.firstName = "foo";
p.lastName  = "bar";
this.context.addToPersons(p);    // That works great

Details d = new Details();
d.phoneNumber = "013245678";
d.adress = "there";
this.context.addToDetails(d); // This didn't work

Of course, the last instruction didn't work as the "personId" is needed.

So the solution would be to get the "p" "id" when the "p" is persisted and then to put it manually in the "personId" column.

How can I do that?

Is is possible to do so :

p.details.addToDetails(d); 

and then give to the personId automatically? If yes, how can I do it?

Thanks,

Regards.

A: 

If you set up your foreign keys properly and the relationships are working, you should be able to simply say:

p.details.Add(d);
Justin Niessner