Я использую отображение nhibernate 3.2 по коду, и у меня странное поведение.
Если я упростил свой проект, у меня есть 2 таблицы:
[Serializable]
public class Intervention
{
public Intervention()
{
ReponsePointVerification = new List<ReponsePointVerification>();
}
#region Public Properties
public virtual int Id
{
get;
set;
}
public virtual IList<ReponsePointVerification> ReponsePointVerification
{
get;
set;
}
}
public class InterventionMap : ClassMapping<Intervention>
{
public InterventionMap()
{
Id<int>(x => x.Id, map =>
{
map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});
Bag(x => x.ReponsePointVerification, map =>
{
map.Key( k => k.Column( "IdIntervention" ) );
});
}
}
и
[Serializable]
public class ReponsePointVerification
{
#region Public Properties
public virtual int Id
{
get;
set;
}
public virtual Intervention Intervention
{
get;
set;
}
}
public class ReponsePointVerificationMap : ClassMapping<ReponsePointVerification>
{
public ReponsePointVerificationMap()
{
Id<int>(x => x.Id, map =>
{
map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});
ManyToOne<Intervention>(x => x.Intervention, map => map.Column("IdIntervention"));
}
}
, которая представляет две таблицы Intervention и ReponsePointVerification, которые имеют внешний ключ (IdIntervention), связанный с Intervention.
Когда я звоню:
return (from interv in Session.Query<Intervention>()
.Fetch(rep => rep.ReponsePointVerification)
select interv).ToList();
или даже когда я неполучить информацию.У меня есть эта ошибка:
Could not cast the value in field id0_ of type Int32 to the Type SerializableType. Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.
Запрос sql выглядит нормально.
Я обнаружил, что сообщение http://groups.google.com/group/nhusers/browse_thread/thread/ef137c3e5b66acdc
И я изменяю класс InterventionMap в соответствии с этимпосле этого он будет работать:
public class InterventionMap : ClassMapping<Intervention>
{
public InterventionMap()
{
Id<int>(x => x.Id, map =>
{
map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});
Bag(x => x.ReponsePointVerification, map =>
{
map.Key( k => k.Column( "IdIntervention" ) );
}, rm => rm.OneToMany());
}
}
Единственное отличие - это добавление ", rm => rm.OneToMany ()"
Поверьте мне, я очень рад, что это сработало, но я действительноне понимаете эту строку, может кто-то объяснить мне смысл?
С уважением
Редактировать
Я сделал WriteAllXmlMapping, и 2 кода:
версия без rm => rm.OneToMany () (которая не работает)
<bag name="ReponsePointVerification">
<key column="IdIntervention" />
<element type="Hyma.BusinessObjets.ReponsePointVerification" />
</bag>
версия с rm => rm.OneToMany ()(какая работа)
<bag name="ReponsePointVerification">
<key column="IdIntervention" />
<one-to-many class="ReponsePointVerification" />
</bag>