简体   繁体   English

.Net中的ArangoDB更新操作

[英]ArangoDB update action in .Net

I am a .Net developer and is currently exploring on ArangoDB. 我是.Net开发人员,目前正在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. 在我深入研究编码细节之前,我一直在使用arangod Web用户界面和arangod,并且非常喜欢这种NoSql。 I could not find the .Net driver working properly. 我找不到.Net驱动程序正常工作。 Even for simple CRUD operation. 即使是简单的CRUD操作。 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) . 它引发了db.Document.Update(employee)的错误。 Here's the error message: Field '_id' does not exist. 这是错误消息:字段'_id'不存在。

Then I tried to add the field _id though I think it is weird, it prompted me another error message. 然后我尝试添加字段_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) ...

I have no clues at all and do not know how to to proceed further. 我一点都不知道,也不知道如何进一步进行。 Any help will be much appreciated. 任何帮助都感激不尽。 Thanks. 谢谢。

This is likely due to the definition of the Employee class, which is not contained in the above snippet. 这可能是由于Employee类的定义所致,该类未包含在上述片段中。

To identify a document in a collection, documents have special system attributes, such as _id , _key and _rev . 为了标识集合中的文档,文档具有特殊的系统属性,例如_id_key_rev These attributes should be mapped to properties in .NET classes, even if not used explicitly. 即使未明确使用,这些属性也应映射到.NET类中的属性。 So one property in the class should be tagged with "Identity", one with "Key", and one with "Revision". 因此,该类中的一个属性应标记为“ Identity”,一个标记为“ Key”,而另一个标记为“ 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: ThisIsId属性将包含自动分配的_id值,并且以后还可以用于轻松检索文档:

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

You can of course rename the properties to your like. 您当然可以将属性重命名为自己喜欢的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM