2

プロパティに属性を設定して、IDプロパティと同じようにこのプロパティを使用し、自動インクリメントを設定するようにRavenDBに指示する方法はありますか?

擬似コード:

public class MyObj {
    public string Id { get; set; }
    [Increment]
    public int OtherProp { get; set; }
}
4

2 に答える 2

5

Dynamicusは正しいソリューションを示していますが、他のStackoverflowユーザーを支援し、ソリューションをより検索しやすくするための正確なサンプルコードを提供したいと思います。

質問のサンプルコードに関連して、解決策は次のとおりです。

public class OtherPropIncrementListener : IDocumentStoreListener
{
    HiLoKeyGenerator _generator;
    IDocumentStore _store;

    public OtherPropIncrementListener(IDocumentStore store)
    {
        this._store = store;
        _generator = new HiLoKeyGenerator(store.DatabaseCommands, "MyObjs", 1);
    }

    public void AfterStore(string key, object entityInstance, RavenJObject metadata)
    {
    }

    public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
    {
        var myObj = entityInstance as MyObj;
        if(myObj != null && myObj.OtherProp == 0)
        {
            string documentKey = _generator.GenerateDocumentKey(_store.Conventions, entityInstance);
            myObj.OtherProp = int.Parse(documentKey.Substring(documentKey.IndexOf("/") + 1));
            return true;
        }

        return false;
    }
}

次に、初期化した後DocumentStore、このコードを追加して上記のリスナーを機能させます。

documentStore.RegisterListener(new OtherPropIncrementListener(documentStore));
于 2012-10-02T09:33:46.500 に答える
0

あなたはRavenDBグーグルグループで答えを見つけることができます。 https://groups.google.com/forum/#!msg/ravendb/70xaVjoMidU/ssDs4mbKoxQJ

于 2012-09-28T16:20:38.287 に答える