我有以下型号:
public class ExchangeRate
{
[Key, Column(Order = 1)]
public virtual int JobId { get; set; }
[Key, Column(Order = 2), MaxLength(3)]
public virtual Currency Source { get; set; }
[Key, Column(Order = 3), MaxLength(3)]
public virtual Currency Target { get; set; }
public virtual decimal Rate { get; set; }
public virtual Job Job { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as ExchangeRate;
if (t == null)
return false;
if (this.Job == t.Job & this.Source == t.Source && this.Target == t.Target)
return true;
return false;
}
public override int GetHashCode()
{
return (this.JobId + "|" + this.Source + "|" + this.Target).GetHashCode();
}
我有以下AutoMapping覆盖:
public void Override(AutoMapping<ExchangeRate> mapping)
{
mapping.Map(x => x.Source).CustomType<GenericEnumMapper<Currency>>();
mapping.Map(x => x.Target).CustomType<GenericEnumMapper<Currency>>();
// Define the composite key
mapping.CompositeId()
.KeyProperty(e => e.JobId, "JobId")
.KeyProperty(e => e.Source)
.KeyProperty(e => e.Target);
}
我导出的以下导出的映射文件试图帮助诊断问题:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="JFS.Data.Model.ExchangeRate, JFS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="ExchangeRates">
<composite-id>
<key-property name="JobId" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="JobId" />
</key-property>
<key-property name="Source" type="FluentNHibernate.Mapping.GenericEnumMapper`1[[JFS.Data.Currency, JFS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.3.0.717, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
<column name="Source" />
</key-property>
<key-property name="Target" type="FluentNHibernate.Mapping.GenericEnumMapper`1[[JFS.Data.Currency, JFS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.3.0.717, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
<column name="Target" />
</key-property>
</composite-id>
<property name="Source" type="FluentNHibernate.Mapping.GenericEnumMapper`1[[JFS.Data.Currency, JFS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.3.0.717, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
<column name="Source" />
</property>
<property name="Target" type="FluentNHibernate.Mapping.GenericEnumMapper`1[[JFS.Data.Currency, JFS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.3.0.717, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
<column name="Target" />
</property>
<property name="Rate" type="System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Rate" />
</property>
<many-to-one class="JFS.Data.Model.Job, JFS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Job">
<column name="JobId" />
</many-to-one>
</class>
</hibernate-mapping>
我现在大部分时间都在撕扯我的头发,试图解决这个问题但没有成功。我阅读了很多帖子,但未能成功应用任何解决方案。我对NHibernate相对较新,并且热衷于开始更多地使用它,但到目前为止,已经证明了一个非常陡峭的学习曲线来启动和运行项目。任何关于错误的解决方案和解释的建议都将不胜感激。
答案 0 :(得分:1)
对于属于主键的多对一关系,请使用KeyReference
代替KeyPropery
。
.KeyReference(e => e.Source)
.KeyReference(e => e.Target);