2013-07-03 70 views
0

我有兩個類如何在FluentNHibernate中映射類?

public class ProgramObject 
{ 
    public virtual long Id { get; set; } 
    public virtual string Name { get; set; } 
    public virtual Commisioning planCommisioning { get; set; } 
    public virtual Commisioning factCommisioning { get; set; } 
} 
public class Commisioning 
{ 
    public virtual long Id { get; set; } 
    public virtual decimal? FirstQuarter { get; set; } 
    public virtual ProgramObject planCommisioningRef { get; set; } 
    public virtual ProgramObject factCommisioningRef { get; set; } 
} 

和地圖類

public class ProgramObjectMap : ClassMap<ProgramObject> 
{ 
    public ProgramObjectMap() 
    { 
     Table("Ipo_ProgramObject"); 
     Id(x => x.Id).GeneratedBy.Increment(); 
     Map(x => x.SuplyOrgName).Column("SuplyOrgName"); 
     References(x => x.factPowerCommisioning).Column("factCommisioningID"); 
     References(x => x.planPowerCommisioning).Column("planCommisioningID"); 
    } 
} 
public class CommisioningMap : ClassMap<Commisioning> 
    { 
     public CommisioningMap() 
     { 
      Table("Ipo_Commisioning"); 
      Id(x => x.Id).GeneratedBy.Increment(); 
      Map(x => x.FirstQuarter).Column("FirstQuarter"); 
      HasOne(x => x.factCommisioningRef) 
       .Cascade.All(); 
      HasOne(x => x.planCommisioningRef) 
       .Cascade.All(); 
     } 
    } 

當我嘗試提交我有一個例外「對象引用一個未保存的瞬態的實例」。我在做什麼做錯了映射?

要保存對象我用這個代碼:

private static void Save(SessionManager sManager, ProgramObject ProgramObject, IpoConversions conversions) 
    { 
     sManager.OpenSession(); 
      var currSession = sManager.CurrentSession; 
     using (ITransaction tx = currSession.BeginTransaction()) 
     { 
      currSession.Save(ProgramObject); 
      tx.Commit(); 
     } 
    } 

我認爲,問題的映射。

+1

保存commisionings請出示您用來保存對象的代碼。 –

+0

顯示有問題 – Antoni

+0

嘗試向'HasOne'映射添加'.Inverse()'。 –

回答

0

您可能需要先設置或級聯

currSession.Save(ProgramObject.factPowerCommisioning); 
currSession.Save(ProgramObject.planPowerCommisioning); 

// or 

References(x => x.factPowerCommisioning).Column("factCommisioningID").Cascade.All(); 
References(x => x.planPowerCommisioning).Column("planCommisioningID").Cascade.All(); 
相關問題