0

这是我的对象和映射:

 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++;

            }

错误消息:{“无法转换类型为'NHibernate.Collection.Generic.PersistentGenericSet 1[Midas_FOREX_Engine.Indicators.SMA]' to type 'System.Collections.Generic.List1 [Midas_FOREX_Engine.Indicators.SMA]'的对象。”}

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

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

谢谢!!

4

1 回答 1

2

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情况选择最佳属性类型/映射。

于 2013-09-29T23:23:41.283 回答