使用流畅的 nHibernate 将组件保存到数据库时出错

本文关键字:保存 数据库 出错 组件 nHibernate | 更新日期: 2023-09-27 18:33:19

这是我的对象和映射:

 public class candleStim
{
    public virtual int       Id            { get; set; }
    public virtual int       candleNumber  { get; set; } //the number of the candle from the data set, should correspond with number of minutes into testing on 1 min candles
    public virtual DateTime  date          { get; set; }
    public virtual decimal   open          { get; set; }
    public virtual decimal   high          { get; set; }
    public virtual decimal   low           { get; set; }
    public virtual decimal   close         { get; set; }
    public virtual List<EMA> EMAs          { get; set; } //List all EMAs calculated.
    public virtual List<SMA> SMAs          { get; set; }
}
    public class candleStimMap : ClassMap<candleStim>
    {
        public candleStimMap()
        {
            Id(x => x.Id);
            Map(x => x.candleNumber);
            Map(x => x.date);
            Map(x => x.open);
            Map(x => x.high);
            Map(x => x.low);
            Map(x => x.close);

            HasMany<SMA>(x => x.SMAs)
                .Component(c =>
                    {
                        c.Map(x => x.SimpleMovingAverage);
                        c.Map(x => x.periods);
                    }).AsSet();

            HasMany<EMA>(x => x.EMAs)
              .Component(c =>
              {
                  c.Map(x => x.ExponentialMovingAverage);
                  c.Map(x => x.periods);
              }).AsSet();
            Table("candle_Simulation");

        } //end public candleStimMap()

这是我目前的保存尝试(失败)

     foreach (candleStim c in calculatedCandles)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        candleStim cc = new candleStim();
                        cc.date = c.date;
                        cc.open = c.open;
                        cc.high = c.high;
                        cc.low = c.low;
                        cc.close = c.close;
 //The below 2 lines are where the problem arises
//if these are standard objects, no errors show up
                        cc.EMAs = c.EMAs;
                        cc.SMAs = c.SMAs;

                        session.Save(c);
                        transaction.Commit();
                    }
                }
                counter++;
            }

错误 msg:{"无法强制转换类型为'NHibernate.Collection.Generic.PersistentGenericSet 1[Midas_FOREX_Engine.Indicators.SMA]' to type 'System.Collections.Generic.List 1[Midas_FOREX_Engine.Indicators.SMA]'的对象。

所以我的列表类型不匹配。如何列出 NHibernate.Collection.Generic.PersistentGenericSet 类型并保存值?

我从 SMA-EMA 保存到数据库的唯一字段是值的小数和周期数的整数。

谢谢!!

使用流畅的 nHibernate 将组件保存到数据库时出错

HasMany 属性映射到 AsSet 时,应对其使用 ISet 类型。

所以你的财产会变成:

public virtual Iesi.Collections.ISet<EMA> EMAs          { get; set; }

我强烈建议您阅读此NHibernate文档章节(如果您仍然没有)。

http://nhibernate.info/doc/nh/en/index.html#collections-persistent

它将阐明您如何选择最佳的属性类型/映射到您的one-to-many情况。