2010-08-21 13 views
1

我有一個帶有代理Id的實體和一個與FluentNHibernate映射的Composite NaturalId。我使自然ID可變標記爲「Not.ReadOnly()」。喜歡的東西:FluentNHibernate中的Mutable NaturalId在修改時拋出異常

public class EntityMap: ClassMap<Entity> 
    { 
     public EntityMap() 
     { 
      Id(x => x.Id); 

      NaturalId().Not.ReadOnly() 
       .Reference(x => x.OtherEntity, "OtherEntityId") 
       .Property(x => x.IntegerProperty); 

      // more fields 
     } 
    } 

生成的XML是這樣的:

<natural-id mutable="true"> 
     <property name="IntegerProperty" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="IntegerProperty" /> 
     </property> 
     <many-to-one class="OtherEntity, OtherEntity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="OtherEntity"> 
     <column name="OtherEntityId" /> 
     </many-to-one> 
    </natural-id> 

如果我改變OtherEntity,操作正常工作和實體在數據庫中更新。如果我改變了IntegerPropery,我得到了異常:「Namespace.Entity實例的不可變自然標識符被改變了」。

爲什麼它會抱怨「不可變的自然標識符」,如果它被標記爲mutable =「true」?

的代碼是一樣的東西:

using (var session = SessionManager.OpenSession()) 
using (var tran = session.BeginTransaction()) 
{ 
    session.Update(entity); 

    entity.IntegerProperty = (int)differentValue; 

    tran.Commit(); 
} 

感謝

回答

0

屬性是.Not.ReadOnly默認。

這是默認情況下不可變的自然編號。

對於XML映射,您必須指定mutable="true"。在Fluent中查找類似的東西(我不確定它是否支持)

+1

檢查由流暢生成的hbm我可以看到Not.Readonly()設置mutable =「true」爲自然id標記,但它仍然失敗。所以,它似乎是NHibernate級別的問題,而不是流利的映射。我更新了問題以添加更多信息。 – 2010-08-21 23:14:52

+0

我相信你最好的選擇是去掉natural-id並直接在課堂上留下兩個屬性。無論如何,這並不重要。 – 2010-08-22 10:48:16

+0

謝謝迭戈。我在最後做的是刪除實體並創建一個新實體。在這種特殊情況下,從業務邏輯的角度來看,這實際上是有意義的,否則我會按照你所說的可能是一個評論說明那些屬性形成一個自然ID就足夠了。再次感謝你的幫助。 – 2010-08-29 15:36:20