在nhibernate中将错误映射到多个错误

时间:2012-08-23 14:35:47

标签: c# fluent-nhibernate

HY, 我有这个c#代码:

 var criteria = GetSession().CreateCriteria<Indicatore>();

 List<string> dpsirs  //passed as a parameter

 // I want to select all [indicatore] that have [dpsrir] in my list dpsirs
 if (dpsirs.Count > 0) {
   criteria.CreateCriteria("IndicatoriDpsir", "Dpsir", JoinType.InnerJoin);
   criteria.Add(Restrictions.In("Dpsir.Dpsir", dpsirs)); 
 }

当我选择数据时

var indicatori = criteria.List<Indicatore>();

我有这个错误:

Exception occurred getter of Mappings.Dpsir.DpsirId
Object does not match target type.

我的映射类是:

public class Indicatore {
....
public virtual IList<IndicatoreDpsir> IndicatoriDpsir { get; set; }
....
}

public class IndicatoreMap : ClassMap<Indicatore> {
....
this.HasMany(x => x.IndicatoriDpsir).KeyColumn("INDICATORE_PFK");
}

public class IndicatoreDpsir {
  public virtual Indicatore Indicatore { get; set; }
  public virtual Dpsir Dpsir { get; set; }
}

public class IndicatoreDpsirMap : ClassMap<IndicatoreDpsir> 
  public IndicatoreDpsirMap() {
   Table("R_INDICATORI_DPSIR");
   LazyLoad();
   CompositeId()
   .KeyReference(x => x.Indicatore, "INDICATORE_PFK")
   .KeyReference(x => x.Dpsir, "DPSIR_PID");
  }
}

public class Dpsir {    
  public Dpsir() {
    IndicatoriDpsir = new List<IndicatoreDpsir>();
  }    
  public virtual string DpsirId { get; set; }
  public virtual IList<IndicatoreDpsir> IndicatoriDpsir { get; set; }
  public virtual string DescrizioneIt { get; set; }
  public virtual string DescrizioneFr { get; set; } 
}

public class DpsirMap : ClassMap<Dpsir> {    
 public DpsirMap() {
  Table("D_DPSIR");
  LazyLoad();
  Id(x => x.DpsirId).GeneratedBy.Assigned().Column("DPSIR_ID");
  Map(x => x.DescrizioneIt).Column("DESCRIZIONE_IT").Not.Nullable().Length(128);
  Map(x => x.DescrizioneFr).Column("DESCRIZIONE_FR").Not.Nullable().Length(128);
  HasMany(x => x.IndicatoriDpsir).KeyColumn("DPSIR_PID");
 }
}

注意

奇怪的是,我有一个相同的情况可行。唯一的两个区别是,在我的班级Dpsir中,键是字符串的类型(而不是 int )并且是Assigned(而不是Identity)

有什么建议吗?

由于

萨拉

1 个答案:

答案 0 :(得分:0)

在限制中明确指定ID:"Dpsir.Dpsir.DpsirId", dpsirs.Select(d => d.DpsirId).ToArray()