这是我的对象和映射:
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.List
的对象1 [Midas_FOREX_Engine.Indicators.SMA]'。”}
所以我的列表类型不匹配。我如何制作NHibernate.Collection.Generic.PersistentGenericSet类型的列表并保存值?
我从SMA-EMA保存到数据库的唯一字段是值的小数和周期数的整数。
谢谢!
答案 0 :(得分: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
情况的属性类型/映射。