I have the following model:
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();
}
I have the following AutoMapping override:
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);
}
And the following exported mapping file I have exported to try and help diagnose the issue:
<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>
I have been tearing my hair out for most of the day now trying to resolve this issue with no success. I’ve read many posts and not been able to apply any solutions successfully. I am relatively new to NHibernate and keen to start using it more but so far there has proved a pretty steep learning curve to get a project up and running. Any advise on a resolution and explanation for the error would be appreciated.
Use
KeyReferenceinstead ofKeyProperyfor many to one relationships that are part of the primary key.