当使用引用的集合时,Fluent Nhibernate引用了不正确的列名

本文关键字:引用 Nhibernate 不正确 Fluent 集合 | 更新日期: 2023-09-27 18:06:29

我是Fluent NHibernate的新手,使用一些相对简单的对象关联。我有一个Fund对象,它有Holding对象的成员集合。我自己明确地编写了所有的对象和映射文件,而不是使用Automapper。当试图检索基金对象的Holding集合时,我得到以下异常。似乎NHibernate使用了一个不正确的列名称为holding _fundId属性,我相信我已经在holding映射的引用中显式设置了。我想我遗漏了一些小细节,但我对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");
    }
}

当使用引用的集合时,Fluent Nhibernate引用了不正确的列名

这与默认的FK约定有关,fluent之前已经以不同的形式在SO上被要求过。例如:

  • Fluent NHibernate存在多外键映射问题
  • 我可以做一个流利的NHibernate外键约定,其中包括父键名?

和关于所有约定的官方wiki:

  • http://wiki.fluentnhibernate.org/Available_conventions

更完整的是,您也可以通过在HasMany中显式指定键列来解决它,而不需要约定(如链接文章中提到的),例如:

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

显示您的密钥名称,如下所示:

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