Why is Fluent NHibernate ignoring my unique constraint on a component?

匆匆过客 提交于 2019-12-07 06:19:53

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!