通过代码配置 RavenDB 版本控制 [英] Configure RavenDB versioning via code

查看:46
本文介绍了通过代码配置 RavenDB 版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过代码在新的 RavenDB 数据库上配置版本控制?

Is it possible to configure versioning on a new RavenDB database via code?

我正在使用以下代码(基于 http://ravendb.net/docs/2.0/server/extending/bundles/versioning):

I'm using the following code (based on http://ravendb.net/docs/2.0/server/extending/bundles/versioning):

Store.DatabaseCommands.EnsureDatabaseExists(database);
using (var session = Store.OpenSession(database))
{
    session.Store(new 
        {
            Exclude = false,
            Id = "Raven/Versioning/DefaultConfiguration",
            MaxRevisions = 5
        });
    session.SaveChanges();
}

但是当我在数据库中添加和修改记录时,版本控制不起作用.

But when I add and modify records in the database versioning is not working.

推荐答案

这段代码很好,但它只是创建了 Versioning Bundle 将要查找的配置信息.它实际上并没有启用捆绑包.

That code is good, but it just creates the configuration information that the Versioning Bundle will look for. It doesn't actually enable the bundle.

对于任何 named 包,您可以通过在 Raven/ActiveBundles 设置中包含名称来启用它,这是一个以分号分隔的包名称列表.

For any named bundle, you enable it by including the name in the Raven/ActiveBundles setting, which is a semicolon-delimited list of bundle names.

命名"包是指那些使用 [ExportMetadata] 属性导出 Bundle" 名称的包.所有内置包都这样做.(如果您深入研究 其中一个触发器的源代码).

By "named" bundles, I mean those that export a "Bundle" name using the [ExportMetadata] attribute. All of the built-in bundles do this. (You can see that the Versioning Bundle exports the name "Versioning" if you dig into the source code of one of its triggers).

如果一个包是未命名,那么它总是处于启用状态,只要它存在于 RavenDB 服务器程序集中,或者存在于 \plugins 文件夹中的单独程序集中.

If a bundle is unnamed then it is always enabled, as long as it exists either within the RavenDB server assemblies, or in a separate assembly in the \plugins folder.

看起来文档需要更新,因为它仍然说将 Raven.Bundles.Versioning.dll 程序集放在插件文件夹中.它不再存在,因为它在 2.0 中被移到了主要的 RavenDB 服务器程序集中.因此,对于这个特定的包,只需编辑设置就足够了.

It looks like the documentation needs updating, as it still says to place the Raven.Bundles.Versioning.dll assembly in the plugins folder. That no longer exists, as it was moved into the main RavenDB server assemblies in 2.0. So for this particular bundle, just editing the settings will be sufficient.

命名租户数据库的设置保存在系统数据库中名为 Raven/Databases/ 的文档中.只需编辑此文档一次,即可激活捆绑包.这是一个可以为您做到这一点的扩展方法:

The settings for a named tenant database are kept in the system database in a document called Raven/Databases/<YourDatabaseName>. Simply edit this document once, and the bundle is activated. Here is an extension method that will do that for you:

public static void ActivateBundle(this IDocumentStore documentStore, string bundleName, string databaseName)
{
    using (var session = documentStore.OpenSession())
    {
        var databaseDocument = session.Load<DatabaseDocument>("Raven/Databases/" + databaseName);

        var settings = databaseDocument.Settings;
        var activeBundles = settings.ContainsKey(Constants.ActiveBundles) ? settings[Constants.ActiveBundles] : null;
        if (string.IsNullOrEmpty(activeBundles))
            settings[Constants.ActiveBundles] = bundleName;
        else if (!activeBundles.Split(';').Contains(bundleName, StringComparer.OrdinalIgnoreCase))
            settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;

        session.SaveChanges();
    }
}

使用上面的方法,你可以简单地调用:

Using the above method, you can simply call:

documentStore.ActivateBundle("Versioning", "YourDatabaseName");

如果您运行的是嵌入式模式数据库,则没有命名的租户数据库,因此过程略有不同.您可以将设置放入您自己的 app.config 文件中,或者您可以在对 documentStore.Initialize() 的现有调用之前操作 documentStore.Configuration.Settings 字典.以下是适用于嵌入式数据库的扩展方法的修订版:

If you are running an embedded-mode database, there are no named tenant databases, so the procedure is a bit different. You can put settings into your own app.config file, or you can manipulate the documentStore.Configuration.Settings dictionary before your existing call to documentStore.Initialize(). Here is a revised version of the extension method that will work on an embedded database:

public static void ActivateBundle(this EmbeddableDocumentStore documentStore, string bundleName)
{
    var settings = documentStore.Configuration.Settings;
    var activeBundles = settings[Constants.ActiveBundles];
    if (string.IsNullOrEmpty(activeBundles))
        settings[Constants.ActiveBundles] = bundleName;
    else if (!activeBundles.Split(';').Contains(bundleName, StringComparer.OrdinalIgnoreCase))
        settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;
}

使用此方法,您可以简单地执行以下操作:

Using this method, you can simply do this:

documentStore.ActivateBundle("Versioning");
documentStore.Initialize();

这篇关于通过代码配置 RavenDB 版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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