在使用引用的集合时,Fluent Nhibernate引用了错误的列名

时间:2011-08-21 22:57:27

标签: c# nhibernate fluent-nhibernate

我是Fluent NHibernate的新手,并且使用了一些相对简单的对象关联。我有一个Fund对象,它有一个Hold对象的成员集合。我自己明确地编写了所有对象和映射文件,而不是使用Automapper。在尝试检索基金对象的Holding集合时,我得到以下异常。似乎NHibernate使用了一个不正确的列名来保存_fundId属性,我相信我已经明确地设置了保持映射中的引用。我想我错过了一些小细节,但我对FNH的诊断不够熟悉。谢谢你的帮助。

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 个答案:

答案 0 :(得分:2)

这与流利的默认FK约定有关,并且之前在SO上以不同的形式提出过。例如,见:

关于所有公约的官方维基:

要完成,您可以在没有约定的情况下解决它(如链接文章中所述),方法是自己在HasMany中明确指定键列:

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

答案 1 :(得分:0)

明确提及您的密钥名称,如下所示:

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