开发者

Adding "second level object" with Entity Framework (C#)

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 "pe开发者_如何学运维rsonId" 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.


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

p.details.Add(d);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜