RavenDb UniqueConstraint 不起作用

RavenDb UniqueConstraint doesn't work

我使用的是 RavenDb 服务器和客户端 3.5.0,我曾尝试让 UniqueConstraint 工作但没有成功。

简单案例:

using Raven.Client.UniqueConstraints;
public class User {
    public string Id { get; set; }
    [UniqueConstraint]
    public string Email { get; set; }
}

文档说:

Drop the Raven.Bundles.UniqueContraints assembly in the Plugins directory.

我是用 NuGet 做的:Install-Package RavenDB.Bundles.UniqueConstraints -Version 3.5.0 然后将二进制 Raven.Bundles.UniqueConstraints.dll 粘贴到我在 Raven 的根目录中自己创建的文件夹 Plugins。

保存用户文档后,我在元数据中得到了这个:

"Ensure-Unique-Constraints": [
        {
            "Name": "Email",
            "CaseInsensitive": false
        }
    ]

一切似乎都有效,但我仍然使用相同的电子邮件保存文档。

UniqueConstraintCheckResult<User> checkResult = session.CheckForUniqueConstraints(user);

                    // returns whether its constraints are available
                    if (checkResult.ConstraintsAreFree())
                    {
                        session.Store(user);
                        session.SaveChanges();
                    }

我检查了这个 link RavenDB UniqueConstraint doesn't seem to work and this one https://groups.google.com/forum/#!searchin/ravendb/unique|sort:relevance/ravendb/KzO-eIf9vV0/NJyJ4DNniFUJ 和许多其他人有同样的问题但没有解决方案。在某些情况下,他们说正在检查 属性 是否已作为解决方案手动存在于数据库中。

文档还说:

To activate unique constraints server-wide, simply add Unique Constraints to Raven/ActiveBundles configuration in the global configuration file, or setup a new database with the unique constraints bundle turned on using API or the Studio

但不知道该怎么做。我做了一些搜索并找到了可能的方法: 在 Studio 中,select 数据库,转到设置 -> 数据库设置,我找到了这个配置:

{
    "Id": "TestRaven",
    "Settings": {
        "Raven/DataDir": "~\TestRaven"
    },
    "SecuredSettings": {},
    "Disabled": false
}

我尝试添加此配置:

"Settings": {
        "Raven/DataDir": "~\TestRaven",
        "Raven/ActiveBundles": "UniqueConstraints"
}

然后我在尝试保存时遇到错误。错误消息显示类似 "the database is already created and cant modify or add bundles" 的内容,并建议添加此行 "Raven-Temp-Allow-Bundles-Change": true 并且我能够使用 UniqueConstraint 捆绑包配置保存 de 设置。

到目前为止,我想我已经完成了文档描述的所有要求。最后一个是:

Any bundle which is not added to ActiveBundles list, will not be active, even if the relevant assembly is in the Plugins directory.

我找到捆绑包列表的唯一地方是在 Studio 中创建一个新数据库,但该列表不可编辑,只是有关已启用内容的信息。

文档说了很多要求,就是不告诉我们怎么做,太聪明了,我们得试试看怎么做。到目前为止我可以到达这里,但是你猜怎么着?还是不行!

我的问题是,UniqueConstraints 在 RavenDb 中真的有效吗?有人让这个工作吗?

如果是,云请告诉我如何?

提前致谢!

[已编辑] 我忘了提到我添加了以下行:

store.Listeners.RegisterListener(new UniqueConstraintsStoreListener());

并且还尝试了 3.5.1 版。

问题是包的指定名称不正确,因此它不会在服务器端处于活动状态。请在 "Raven/ActiveBundles" 设置选项中使用 "Unique Constraints" 而不是 "UniqueConstraints"