实体框架 - 一对一关系,如何保存记录

时间:2010-03-16 01:53:28

标签: asp.net entity-framework linq-to-entities

我在两个表上有一对一的关系,在我的EDM中由2个类表示。

我正在尝试保存新记录,并且很困惑如何保存到第二个实体。我尝试了以下内容。

Dim myObject as new MyObject

myObject.prop1 = 5
myObject.prop2 = "Test"

myObject.myOtherObject.prop1 = 3
myObject.myOtherObject.prop2 = "Hello"

但是当我点击myObject.myOtherObject.prop1 = 3时收到错误,因为myOtherObject尚未初始化。

如何在myObject下初始化子对象?

1 个答案:

答案 0 :(得分:1)

尝试:

Dim myObject as new MyObject

myObject.prop1 = 5
myObject.prop2 = "Test"

myObject.myOtherObject = new MyOtherObject
myObject.myOtherObject.prop1 = 3
myObject.myOtherObject.prop2 = "Hello"