NHibernate UniqueKey 未强制执行

发布于 2024-09-13 05:12:52 字数 1075 浏览 2 评论 0原文

我有一个类,我想使用 Fluent NHibernate 定义一个唯一的键。我认为我已经正确完成了此操作,但是我的单元 tes,t 在其中为相关字段保存了两个具有相同值的对象,保存了这两个项目而没有错误。这就是我所得到的:

映射覆盖:

public class ItemOverride : IAutoMappingOverride<Item>
{
    #region IAutoMappingOverride<Item> Members

    public void Override(AutoMapping<Item> mapping)
    {
        mapping.Map(t => t.Prompt).UniqueKey("UIX_Item_Prompt").Not.Nullable();
    }

    #endregion
}

单元测试

[Test]
public void CannotSaveDuplicateItemPrompt()
{
    ServiceLocatorInitializer.Init();
    IItemManagementService itemManagementService = new ItemManagementService(repository);
    Item first = ItemInstanceFactory.CreateValidTransientItem();
    first.Prompt = "Duplicate";
    itemManagementService.SaveOrUpdate(first);
    Item second = ItemInstanceFactory.CreateValidTransientItem();
    second.Prompt = "Duplicate";

    ActionConfirmation confirm = itemManagementService.SaveOrUpdate(second);

    Assert.IsFalse(confirm.WasSuccessful);
    Assert.AreEqual(confirm.Message, "");
}

I have a class on which I'd like to define a unique key using Fluent NHibernate. I think I've done this correctly, but my unit tes,t in which I save two objects with the same value for the field in question, saves both items without error. Here's what I've got:

Mapping override:

public class ItemOverride : IAutoMappingOverride<Item>
{
    #region IAutoMappingOverride<Item> Members

    public void Override(AutoMapping<Item> mapping)
    {
        mapping.Map(t => t.Prompt).UniqueKey("UIX_Item_Prompt").Not.Nullable();
    }

    #endregion
}

Unit Test

[Test]
public void CannotSaveDuplicateItemPrompt()
{
    ServiceLocatorInitializer.Init();
    IItemManagementService itemManagementService = new ItemManagementService(repository);
    Item first = ItemInstanceFactory.CreateValidTransientItem();
    first.Prompt = "Duplicate";
    itemManagementService.SaveOrUpdate(first);
    Item second = ItemInstanceFactory.CreateValidTransientItem();
    second.Prompt = "Duplicate";

    ActionConfirmation confirm = itemManagementService.SaveOrUpdate(second);

    Assert.IsFalse(confirm.WasSuccessful);
    Assert.AreEqual(confirm.Message, "");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2024-09-20 05:12:53

好的,如果我将两个保存包装在一个事务中,它就可以工作。这是新的单元测试:

    [Test]
    [ExpectedException(typeof(GenericADOException))]
    public void CannotSaveDuplicateItemPrompt()
    {
        ServiceLocatorInitializer.Init();
        IItemManagementService itemManagementService = new ItemManagementService(repository);

        NHibernateSession.Current.BeginTransaction();

        Item first = ItemInstanceFactory.CreateValidTransientItem();
        first.Prompt = "Duplicate";
        itemManagementService.SaveOrUpdate(first);
        Item second = ItemInstanceFactory.CreateValidTransientItem();
        second.Prompt = "Duplicate";

        ActionConfirmation confirm = itemManagementService.SaveOrUpdate(second);
        NHibernateSession.Current.Transaction.Commit();
    }

Okay, it works if I wrap the two saves in a transaction. Here's the new Unit Test:

    [Test]
    [ExpectedException(typeof(GenericADOException))]
    public void CannotSaveDuplicateItemPrompt()
    {
        ServiceLocatorInitializer.Init();
        IItemManagementService itemManagementService = new ItemManagementService(repository);

        NHibernateSession.Current.BeginTransaction();

        Item first = ItemInstanceFactory.CreateValidTransientItem();
        first.Prompt = "Duplicate";
        itemManagementService.SaveOrUpdate(first);
        Item second = ItemInstanceFactory.CreateValidTransientItem();
        second.Prompt = "Duplicate";

        ActionConfirmation confirm = itemManagementService.SaveOrUpdate(second);
        NHibernateSession.Current.Transaction.Commit();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文