.Net 中的 ArangoDB 更新操作

ArangoDB update action in .Net

我是一名 .Net 开发人员,目前正在探索 ArangoDB。我玩过 arangod web 用户界面和 arangod,非常喜欢这个 NoSql,直到我深入研究编码的细节。我找不到正常工作的 .Net 驱动程序。即使是简单的 CRUD 操作。问题来了。

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); 

它抛出了 db.Document.Update(employee) 的错误。这是错误消息:字段“_id”不存在。

然后我尝试添加字段 _id 虽然我觉得这很奇怪,但它提示我另一个错误消息。

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) ...

完全没有头绪,也不知道下一步该怎么办。任何帮助都感激不尽。谢谢。

这可能是由于 Employee class 的定义所致,上面的代码段中未包含该定义。

为了识别集合中的文档,文档具有特殊的系统属性,例如_id_key_rev。这些属性应该映射到 .NET classes 中的属性,即使没有明确使用。因此 class 中的一个 属性 应该标记为 "Identity",一个标记为 "Key",一个标记为 "Revision"。这是一个应该有效的 class 定义示例:

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()
    {
    }
}

ThisIsId 属性 将包含自动分配的 _id 值,也可用于稍后轻松检索文档:

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

您当然可以根据自己的喜好重命名属性。