In my map I have:
Component(
x => x.ExposureKey,
m => {
m.Map(x => x.AsOfDate).Not.Nullable();
m.Map(x => x.ExposureId).Length(30).Not.Nullable();
}
).Unique();
The relevant output from the HBM is
<component name="ExposureKey" insert="true" update="true" optimistic-lock="true" class="Some.Namespace.CreditExposureKey, Some.Namespace, Version=0.0.0.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa">
<property name="AsOfDate" type="System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="AsOfDate" not-null="true"/>
</property>
<property name="ExposureId" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ExposureId" length="30" not-null="true"/>
</property>
</component>
which is clearly missing unique="true"
from the component
definition.
Why is this happening?
rebelliard
Are you using the latest version of Fluent NHibernate? According to James Gregory (Fluent NHibernate contributor), it should work.
// Else, try this hack:
Component(x => x.ExposureKey, m =>
{
m.Map(x => x.AsOfDate).Not.Nullable();
m.Map(x => x.ExposureId).Length(30).Not.Nullable();
}).SetAttribute("unique", "true");
It would also be good to check if the generated SQL actually has the Unique
property set even if the hbm mapping files do not (could be a small bug).
来源:https://stackoverflow.com/questions/3901553/why-is-fluent-nhibernate-ignoring-my-unique-constraint-on-a-component