0

嗨,我有这个 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)并且是分配的(而不是身份)

有什么建议么?

谢谢

萨拉

4

1 回答 1

0

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

于 2012-08-29T08:39:45.907 回答