2011-08-21 59 views
1

我是流利NHibernate的新手,使用一些相對簡單的對象關聯。我有一個Fund對象,它有一個Holding對象的成員集合。我自己寫了所有的對象和映射文件,而不是使用Automapper。當試圖爲基金對象檢索Holding集合時,我得到以下例外。看起來NHibernate爲保留的_fundId屬性使用了一個不正確的列名,我相信我已經明確地在Holding地圖的引用中設置了它。我假設我錯過了一些小細節,但我對FNH診斷不夠熟悉。謝謝你的幫助。流利Nhibernate在引用集合中引用不正確的列名稱

NHibernate.Exceptions.GenericADOException : could not initialize a collection: [ThirteenFeed.Entities.Fund._holdings#1][SQL: SELECT holdings0_.Fund_id as Fund9_1_, holdings0_._holdingID as column1_1_, holdings0_._holdingID as column1_4_0_, holdings0_._filingDate as column2_4_0_, holdings0_._principleAmt as column3_4_0_, holdings0_._shares as column4_4_0_, holdings0_._value as column5_4_0_, holdings0_._fundID as column6_4_0_, holdings0_._assetID as column7_4_0_, holdings0_._assetClassID as column8_4_0_ FROM `Holding` holdings0_ WHERE holdings0_.Fund_id=?]----> MySql.Data.MySqlClient.MySqlException : Unknown column 'holdings0_.Fund_id' in 'field list' 

id列名應該是_fundId請參閱下面的對象和地圖。

public class Fund 
{ 
    public virtual int _fundID {get; protected set;} 
    public virtual string _cik {get; set;} 
    public virtual string _fundName {get; set;} 
    public virtual IList<Holding> _holdings {get; protected set;} 

    public Fund() 
    { 
     _holdings = new List<Holding>(); 
    } 

    public virtual void AddHolding(Holding holding) 
    { 
     _holdings.Add(holding); 
     holding._fund = this; 
    } 
} 

public class Holding 
{ 
    public virtual int _holdingID {get; protected set;} 
    public virtual DateTime _filingDate {get; set;} 
    public virtual int _timePeriod {get; set;} 
    public virtual Fund _fund {get; set;} 
    public virtual Asset _asset {get; set;} 
    public virtual AssetClass _assetClass {get; set;} 
    public virtual int _value {get; set;} 
    public virtual int _shares {get; set;} 
    public virtual int _principleAmt {get; set;} 

    public virtual void SetAssetClass(AssetClass assetClass) 
    { 
     this._assetClass = assetClass; 
    } 

    public virtual void SetAsset(Asset asset) 
    { 
     this._asset = asset; 
    } 
} 

public class FundMap : ClassMap<Fund> 
{ 
    public FundMap() 
    { 
     Id(x => x._fundID).GeneratedBy.Identity(); 
     Map(x => x._cik); 
     Map(x => x._fundName); 
     //TODO: get correct handling of HasMany relationships. 
     HasMany(x => x._holdings) 
      .Inverse() 
      .Cascade.Delete(); 
    } 
} 

public class HoldingMap : ClassMap<Holding> 
{ 
    public HoldingMap() 
    { 
     Id(x => x._holdingID).GeneratedBy.Identity(); 
     Map(x => x._filingDate); 
     Map(x => x._principleAmt); 
     Map(x => x._shares); 
     Map(x => x._value); 
     References(x => x._fund) 
      .Column("_fundID"); 
     References(x => x._asset) 
      .Column("_assetID"); 
     References(x => x._assetClass) 
      .Column("_assetClassID"); 
    } 
} 

回答

2

這與流利的默認FK慣例有關,並且已經在SO上以不同的形式提出過。例如見:

以及所有約定官方的wiki:

是完整的,你也可以解決這個問題而不受的hasMany明確指定鍵列公約(如鏈接文章中提到)喜歡自己:

HasMany(x => x._holdings).KeyColumns.Add("_fundID") 
+0

謝謝,我檢查答案,但我正在尋找主鍵約定,而不是外鍵,感謝您的幫助。 – kirps

0

明確提到你的鍵名,如下圖所示:

HasMany(x => x._holdings).KeyColumns.Add("_fundID") 
相關問題