Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If your class has a property called <code>Id</code>, RavenDB will use that as document id by default.</p> <p>In your case, having an instance of <code>MyItem</code> with <code>Id</code> set to <code>303</code>, means that the document will be saved in the database with an Id of <code>MyItems/303</code>.</p> <p>You can test that easily by modifying the </p> <pre><code>session.Store(new MyItem { Id = 303, Test = "TEST" }, path); </code></pre> <p>line with </p> <pre><code>session.Store(new MyItem { Id = 303, Test = "TEST" }); </code></pre> <p>This will work and the result will be:</p> <p><img src="https://i.stack.imgur.com/aMJt8.png" alt="Saved document"></p> <p>So, the issue is that you are instructing Raven to assign two IDs to your object:</p> <ul> <li><code>MyItems/303</code>, due to the value of the <code>Id</code> property</li> <li><code>mykeypath/44</code>, due to the fact that you're explicitly setting this Id by using the <code>Store</code> overload.</li> </ul> <p>The solution I would apply in this case is to rename the <code>Id</code> property. If that's not possible (ex: the class is already used for other purposes, like a WCF contract), consider creating a new class used solely for RavenDB persistence and map between the two (tools like <a href="https://github.com/AutoMapper/AutoMapper" rel="nofollow noreferrer">AutoMapper</a> make this easy).</p> <h3>Update</h3> <p>I found a way to bend the <code>Id</code> field convention by looking at <a href="https://groups.google.com/forum/#!topic/ravendb/Ly10rvbVrcM" rel="nofollow noreferrer">this thread</a>. In your case, you should do the following prior to opening the session:</p> <pre><code>_documentStore.Conventions.FindIdentityProperty = prop =&gt; { if (prop.DeclaringType == typeof (AuditableEntity)) return prop.Name == "CustomId"; return prop.Name == "Id"; }; </code></pre> <p>What this says is that the property <code>Id</code> defined by the type <code>AuditableEntity</code> should not be used as a document identifier. In all other cases, do the default.</p> <p>I tested it and it works fine, showing two Id fields in the RavenDb studio :) :</p> <p><img src="https://i.stack.imgur.com/01Pwa.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload