2009-09-21 75 views
1

我有一個與聯繫人一對多關係的客戶端表。我已經制定了兩個班的NHibernate的爲NHibernate - 添加到收藏夾的新項目不會持續

在客戶端類

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
assembly="MyAssembly.Core" 
namespace="MyAssembly.Core.Domain" 
> 
<class name="Client" table="CompanyXClients"> 
<id name="ClientId" column="ClientId" unsaved-value="0"> 
<generator class="identity" /> 
</id> 
<property name="CompanyId" column="CompanyId" type="Int32" length="8" /> 
<property name="ClientName" column="ClientName" type="String" length="100" /> 
<property name="Address1" column="Address1" type="String" length="255" /> 
<property name="Address2" column="Address2" type="String" length="255" /> 
<property name="City" column="City" type="String" length="50" /> 
<property name="State" column="State" type="String" length="50" /> 
<property name="CountryCode" column="CountryCode" type="String" length="2" /> 
<property name="Zip" column="Zip" type="String" length="7" /> 
<property name="Phone" column="Phone" type="String" length="20" /> 
<property name="Fax" column="Fax" type="String" length="20" /> 
<property name="EmailAddress" column="EmailAddress" type="String" length="255" /> 
<property name="PayPalId" column="PayPalId" type="String" length="255" /> 
<property name="Notes" column="Notes" type="String" length="255" /> 
<property name="TimeZone" column="TimeZone" type="Decimal" /> 
<set name="ContactPersons" lazy="true" cascade="all"> 
<key column="ClientId" not-null="true"/> 
<one-to-many class="ContactPerson"/> 
</set> 
</class> 

</hibernate-mapping> 

在ContactPerson類

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
assembly="MyAssembly.Core" 
namespace="MyAssembly.Core.Domain" 
> 
<class name="ContactPerson" table="ClientXContactPersons"> 
<id name="ContactPersonId" column="ContactPersonId" unsaved-value="0" type="Int64"> 
<generator class="identity" /> 
</id> 
<property name="ClientId" column="ClientId" type="Int64" length="8" insert="false" update="false" /> 
<property name="FirstName" column="FirstName" type="String" length="50" /> 
<property name="LastName" column="LastName" type="String" length="50" /> 
<property name="Designation" column="Designation" type="String" length="50" /> 
<property name="Mobile" column="Mobile" type="String" length="20" /> 
<property name="Phone" column="Phone" type="String" length="20" /> 
<property name="EmailAddress" column="EmailAddress" type="String" length="255" /> 
<property name="IsDefault" column="IsDefault" type="Boolean" /> 
<many-to-one name="Client" column="ClientId" not-null="true"/> 
</class> 

</hibernate-mapping> 

我使用溫莎城堡的NHibernate的。這是保存客戶的代碼。

IClientDAO oClientDao = Factory.GetClientDAO(); 
Client oClient = null; 
if (Form[this.ControlPrefix + "." + "ClientId"] != "0") 
{ 
    oClient = oClientDao.GetById(long.Parse(Form["ClientId"]),true); 
} 
else 
{ 
    oClient = new Client(); 
} 
this.UpdateModel(oClient,"SomePrefix",Form.ToValueProvider()); 
oClient.CompanyId = AuthUser.CompanyId; 
oClientDao.SaveOrUpdate(oClient); 

我設置插入=「假」更新=「假」的客戶端Id(在ContactPerson外鍵),以從插入語句產生額外的列停止NHibernate的。

問題是模型從FormCollection中正確更新。如果我添加一個新客戶端,當我更新客戶端並且級聯工作正常時,聯繫人也會持續存在。但是,如果我編輯現有的客戶端,它將加載現有的聯繫人,從FormCollection更新數據。但更新客戶端不會級聯並執行聯繫人更新。因此,新聯繫人永遠不會被保存,甚至現有聯繫人的數據更改也不會保存。

如果Insert更新並且在更新客戶端時級聯到Contactpersons。更新時必須解決的問題是什麼?

我還設置聯繫人對象的客戶端屬性在集合的setter中的集合正在執行。

public virtual ISet<ContactPerson> ContactPersons 
{ 
    get { return _ContactPersons; } 
     set 
     { 
      _ContactPersons = value; 
      foreach (ContactPerson oPerson in _ContactPersons) 
      { 
       oPerson.Client = this; 
       oPerson.ClientId = this.ClientId; 
      } 
     } 
    } 

我已經嘗試了我在網上發現的帖子中的所有內容。現在對此感到沮喪。任何人都可以幫忙嗎?

回答

-1

我不確定這是否是問題,但我們有類似的問題,我們發現,每當我們使用通用的IList集合,級聯機制不起作用。所以我們使用非仿製收藏

+0

爲什麼downvote?似乎是一個合理的建議。 – 2009-09-21 13:45:38

+0

:)同樣的問題在這裏! – Beatles1692 2009-09-21 20:40:22

+0

我沒有使用通用列表。檢查我的帖子中的代碼,它使用ISet Protean 2009-09-22 11:27:48