简体   繁体   中英

Error mapping many to many in nhibernate

Hy, I have this c# code:

 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)); 
 }

When I select data

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

I have this error:

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

My mapping class are:

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");
 }
}

Note

The strange thing is that I have an identical situation that works. The only 2 differences are that in my class Dpsir the key is type of string (and not int ) and is Assigned (and not Identity)

Any suggestions?

Thanks

Sara

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM