1

I am new to Fluent NHibernate and working with some relatively simple object associations. I have a Fund object which has a member collection of Holding objects. I've written all of the object and mapping files explicitly myself rather than using the Automapper. When trying to retrieve the Holding collection for a fund object I get the following exception. It appears that NHibernate is using an incorrect column name for the holding _fundId property, which I believed I had explicitly set in the reference in the Holding map. I assume I am missing some small detail, but I am not familiar enough with FNH to diagnose. Thanks for your help.

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'

The id column name should be _fundId See objects and maps below.

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");
    }
}
kirps
  • 1,345
  • 2
  • 17
  • 28

2 Answers2

2

This has to do with the default FK conventions that fluent has and has been asked in different forms on SO before. See for instance:

And the official wiki about all conventions:

To be complete you can resolve it also without a convention (as mentioned in the linked articles) by specifying the key column explicitly in the HasMany yourself like:

HasMany(x => x._holdings ).KeyColumns.Add("_fundID")
Community
  • 1
  • 1
Eddy
  • 5,320
  • 24
  • 40
  • Thanks, I checked for answers but I was looking for the primary key convention and not the foreign key, thanks for the help. – kirps Aug 21 '11 at 23:49
0

Mention your key name explicitly as shown below:

HasMany(x => x._holdings ).KeyColumns.Add("_fundID")
Oded
  • 489,969
  • 99
  • 883
  • 1,009
dilip
  • 1