为什么 Fluent NHibernate 会忽略我对组件的唯一约束? [英] Why is Fluent NHibernate ignoring my unique constraint on a component?

查看:25
本文介绍了为什么 Fluent NHibernate 会忽略我对组件的唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的地图中,我有:

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

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>

component 定义中明显缺少 unique="true".

为什么会这样?

推荐答案

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

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");

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

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).

这篇关于为什么 Fluent NHibernate 会忽略我对组件的唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆