0

I'm trying to save user code scripts in RavenDB, and so far almost everything works. I got a script in from a separate process, and I am able to load the script, edit it, and save it back to the database.

The problem comes when I create a new script and try to insert it into the database. I use the code below.

public static void AddUserScript(UserScripts entity, string RavendbUrl, int userid)
{
    try
    {
    IvcRavenDB raven = new IvcRavenDB();
    var store = raven.InitRavenDB(RavendbUrl, "UserScripts");

    entity.Updated = DateTime.Now.ToString();
    entity.Created = DateTime.Now.ToString();
    entity.ScriptFileLength = entity.Script.Length;
    entity.userid = userid;

    using (var session = store.OpenSession())
    {
        session.Store(entity); // DIES HERE
        session.SaveChanges();
    }
    }
    catch (Exception ex)
    {
    var mess = ex.Message;
    }
}

When I say dies, the next line is not executed, there is no exception, and I see no error anywhere, which is very odd. Almost the EXACT same code works to save an entity with an Id, the only difference is that the Id field when adding is null.

I'm using RavenDB 4 with Raven Client 4.02 in ,Net Core 2.0.

I'm very new to Raven and don't even know where to start to figure this out without errors, so any help would be great.

Fallon
  • 45
  • 1
  • 4
  • How'd you know there are no errors? You would swallow them in your catch-block. – Christian.K Apr 06 '18 at 07:01
  • I would actually have to enter the catch, but as I said, it enters session.Store and NEVER returns. – Fallon Apr 06 '18 at 07:08
  • I just ran a test and set the Id to a value, and it worked without issue. However, I thought the procedure was to make the Id field null so that raven would generate it for you. – Fallon Apr 06 '18 at 09:18
  • One final comment and I'm off to bed. I set the Id field to String.Empty, and ravendb generated a GUID and saved successfully. I really don't care how the Id becomes unique, I just need it to be unique, so the bigger question is if there **is any advantage to having the ravendb generated Id's over a GUID**? – Fallon Apr 06 '18 at 09:33

1 Answers1

0

For whatever reason, leaving the Id null is causing a problem, it may be the version of the client, I'm not sure. However I found two documents that describe a few ways to get the Id generated, and they all work consistently.

4 ways to get the Id

  1. Semantic ID This ID was generated by entering the actual string in the document ID. i.e. "users/ayende@hr.com"

  2. Server-Side ID This ID was generated by entering: "users/" in the document ID.

  3. Identity This ID was generated by entering: "users|" in the document ID.

  4. GUID This ID was generated by leaving the document ID field empty

More information can be found here: Document Identifier Generation

Fallon
  • 45
  • 1
  • 4