【问题标题】:ArangoDB update action in .Net.Net 中的 ArangoDB 更新操作
【发布时间】:2015-01-07 04:01:46
【问题描述】:

我是一名 .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) ...

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

【问题讨论】:

    标签: arangodb


    【解决方案1】:

    这很可能是由于Employee类的定义,上面的sn-p中没有包含该类。

    为了识别集合中的文档,文档具有特殊的系统属性,例如_id_key_rev。这些属性应该映射到 .NET 类中的属性,即使没有显式使用。所以类中的一个属性应该用“Identity”标记,一个用“Key”标记,一个用“Revision”标记。这是一个应该工作的示例类定义:

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

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

    【讨论】:

    • 感谢 stj。很难找到正确的 .Net 驱动程序文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2011-02-12
    • 1970-01-01
    相关资源
    最近更新 更多