0
海蘭, 我有這樣的C#代碼:錯誤映射多對多在NHibernate的
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");
}
}
注意
奇怪的是,我有一個工作相同的情況下。只有2間的區別是在我的課DPSIR關鍵是串(而不是INT)的類型和分配(而不是身份)
有什麼建議?
感謝
薩拉
你可以試試' 「Dpsir.Dpsir.DpsirId」,dpsirs.Select(d => d.Dpsir)。 ToArray的()'? – Firo
@Firo - 感謝您的回覆。我現在就解決了。解決方法是(如你所建議的)用'「Dpsir.Dpsir.DpsirId」'替換「Dpsir.Dpsir」。因爲密鑰是** string **的類型。如果鍵是Int類型,則不需要指定鍵列! – sara