我在Person对象和Address对象之间有一个简单的Parent / Child关系。 Person对象存在于DB中。在对Person进行Get之后,我将一个新的Address对象添加到父对象的子对象列表中,并对Person对象进行一些其他更新。最后,我对Person对象进行了更新。使用SQL跟踪窗口,我可以看到对Person表的Person对象的更新和Address表的Insert地址记录。
问题在于,执行更新后,AddressId(Address对象上的主键)仍设置为0,这是第一次初始化Address对象时的默认值。我已经验证当我执行添加时,此值已正确设置。当尝试添加子对象作为NHibernate UPDATE的一部分时,这是一个已知问题吗?示例代码和映射文件位于
之下谢谢
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BusinessEntities.Wellness.Person,BusinessEntities.Wellness" table="Person" lazy="true" dynamic-insert="true" dynamic-update="false">
<id name="Personid" column="PersonID" type="int">
<generator class="native" />
</id>
<version type="binary" generated="always" name="RecordVersion" column="`RecordVersion`"/>
<property type="int" not-null="true" name="Customerid" column="`CustomerID`" />
<property type="AnsiString" not-null="true" length="9" name="Ssn" column="`SSN`" />
<property type="AnsiString" not-null="true" length="30" name="FirstName" column="`FirstName`" />
<property type="AnsiString" not-null="true" length="35" name="LastName" column="`LastName`" />
<property type="AnsiString" length="1" name="MiddleInitial" column="`MiddleInitial`" />
<property type="DateTime" name="DateOfBirth" column="`DateOfBirth`" />
<bag name="PersonAddresses" inverse="true" lazy="true" cascade="all">
<key column="PersonID" />
<one-to-many class="BusinessEntities.Wellness.PersonAddress,BusinessEntities.Wellness" /
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="BusinessEntities.Wellness.PersonAddress,BusinessEntities.Wellness" table="PersonAddress" lazy="true" dynamic-insert="true" dynamic-update="false">
<id name="PersonAddressId" column="PersonAddressID" type="int">
<generator class="native" />
</id>
<version type="binary" generated="always" name="RecordVersion" column="`RecordVersion`" />
<property type="AnsiString" not-null="true" length="1" name="AddressTypeid" column="`AddressTypeID`" />
<property type="AnsiString" not-null="true" length="60" name="AddressLine1" column="`AddressLine1`" />
<property type="AnsiString" length="60" name="AddressLine2" column="`AddressLine2`" />
<property type="AnsiString" length="60" name="City" column="`City`" />
<property type="AnsiString" length="2" name="UsStateId" column="`USStateID`" />
<property type="AnsiString" length="5" name="UsPostalCodeId" column="`USPostalCodeID`" />
<many-to-one name="Person" cascade="none" column="PersonID" />
</class>
</hibernate-mapping>
Person newPerson = new Person();
newPerson.PersonName = "John Doe";
newPerson.SSN = "111111111";
newPerson.CreatedBy = "RJC";
newPerson.CreatedDate = DateTime.Today;
personDao.AddPerson(newPerson);
Person updatePerson = personDao.GetPerson(newPerson.PersonId);
updatePerson.PersonAddresses = new List<PersonAddress>();
PersonAddress addr = new PersonAddress();
addr.AddressLine1 = "1 Main St";
addr.City = "Boston";
addr.State = "MA";
addr.Zip = "12345";
updatePerson.PersonAddresses.Add(addr);
personDao.UpdatePerson(updatePerson);
int addressID = updatePerson.PersonAddresses[0].AddressId;
答案 0 :(得分:0)
尝试:
updatePerson.PersonAddresses = new List<PersonAddress>();
PersonAddress addr = new PersonAddress();
addr.AddressLine1 = "1 Main St";
addr.City = "Boston";
addr.State = "MA";
addr.Zip = "12345";
updatePerson.PersonAddresses.Add(addr);
addr.Person = updatePerson
通常的做法是公开用于管理集合的添加和删除方法,例如
public void AddAddress(Address addr)
{
PersonAddresses.Add(addr);
addr.Person = this;
}