为什么Fluent NHibernate忽略了对组件的唯一约束?

jas*_*son 5 nhibernate components fluent-nhibernate unique-constraint

在我的地图中我有:

Component(
    x => x.ExposureKey,
    m => {
        m.Map(x => x.AsOfDate).Not.Nullable();
        m.Map(x => x.ExposureId).Length(30).Not.Nullable();
    }
).Unique();
Run Code Online (Sandbox Code Playgroud)

HBM的相关产出是

<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>
Run Code Online (Sandbox Code Playgroud)

unique="true"component定义中显然是缺失的.

为什么会这样?

reb*_*ard 0

您使用的是最新版本的 Fluent NHibernate 吗?根据James Gregory(Fluent NHibernate 贡献者)的说法,它应该可以工作。

// 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");
Run Code Online (Sandbox Code Playgroud)

Unique即使 hbm 映射文件没有(可能是一个小错误),也最好检查生成的 SQL 是否确实设置了属性。