2
votes

I am a .Net developer and is currently exploring on ArangoDB. I have played around with the arangod web user interface and arangod and like this NoSql very much until I delve into the detail of coding. I could not find the .Net driver working properly. Even for simple CRUD operation. Here's the problem.

ArangoClient.AddConnection("127.0.0.1", 8529, false, "Sample", "Sample");

var db = new ArangoDatabase("Sample");

string collectionName = "MyTestCollection";
var collection = new ArangoCollection();
collection.Name = collectionName;
collection.Type = ArangoCollectionType.Document;

if (db.Collection.Get(collectionName) == null)
{
    db.Collection.Create(collection);
}

var employee = new Employee();

employee.Id = "1234";
employee.Name = "My Name";
employee.Salary = 33333;
employee.DateOfBirth = new DateTime(1979, 7, 22);

db.Document.Create<Employee>("MyTestCollection", employee);

employee.Name = "Tan";
db.Document.Update(employee); 

It thrown the error for db.Document.Update(employee). Here's the error message: Field '_id' does not exist.

Then I tried to add the field _id though I think it is weird, it prompted me another error message.

Arango.Client.ArangoException : ArangoDB responded with error code BadRequest:
expecting PATCH /_api/document/<document-handle> [error number 400]
   at Arango.Client.Protocol.DocumentOperation.Patch(Document document, Boolean waitForSync, String revision)
   at Arango.Client.ArangoDocumentOperation.Update[T](T genericObject, Boolean waitForSync, String revision) ...

I have no clues at all and do not know how to to proceed further. Any help will be much appreciated. Thanks.

1

1 Answers

4
votes

This is likely due to the definition of the Employee class, which is not contained in the above snippet.

To identify a document in a collection, documents have special system attributes, such as _id, _key and _rev. These attributes should be mapped to properties in .NET classes, even if not used explicitly. So one property in the class should be tagged with "Identity", one with "Key", and one with "Revision". Here is an example class definition that should work:

public class Employee
{
    /* this will map the _id attribute from the database to ThisIsId property */
    [ArangoProperty(Identity = true)]
    public string ThisIsId { get; set; }

    /* this will map the _key attribute from the database to the Id property */
    [ArangoProperty(Key = true)]
    public string Id { get; set; }

    /* here is _rev */        
    [ArangoProperty(Revision = true)]
    public string ThisIsRevision { get; set; }

    public DateTime DateOfBirth { get; set; }
    public string Name { get; set; }
    public int Salary { get; set; }

    public Employee()
    {
    }
}

The ThisIsId property will contain the automatically assigned _id value, and can also be used to retrieve the document easily later:

var employeeFromDatabase = db.Document.Get<Employee>(employee.ThisIsId);

You can of course rename the properties to your like.