Upsert complex data

Is there any sample to upsert document in couchbase using dotnet sdk?
Tried the following code, but it doesn’t recognize the portion LocationAddress onwards down.

var doc = new Document
{
Id = myID,
Content = new
{
Active= “1”,
TemplateID= “3”,
ChargeCode= “4072”,
CreatedBy= “test”,
CreatedOn= “2017-07-24 17:12:40”,
DepartmentDescr= “test”,
Email= "test@test.com",
EmployeeID= “123”,
FirstName= “test”,
IsOrdered= “1”,
LastName= “test”,
LocationAddress = [
{
AddressInfo= “5004 Railroad Ave.”,
Class= “Address”,
Name= “Address1”
},
{
AddressInfo= “test.”,
Class= “Address”,
Name= “Address2”
},

@rkbnair

You should probably create actual POCO classes for your document and the subobjects that make it up. Then you could use code like this to build the document:

var doc = new Document<Customer>
{
    Id = myID,
    Content = new Customer
    {
        Active= “1”,
        TemplateID= “3”,
        ChargeCode= “4072”,
        CreatedBy= “test”,
        CreatedOn= “2017-07-24 17:12:40”,
        DepartmentDescr= “test”,
        Email= "test@test.com",
        EmployeeID= “123”,
        FirstName= “test”,
        IsOrdered= “1”,
        LastName= “test”,
        LocationAddress = new List<Address>
        {
            new Address
            {
                AddressInfo= “5004 Railroad Ave.”,
                Class= “Address”,
                Name= “Address1”
            },
            new Address
            {
                AddressInfo= “test.”,
                Class= “Address”,
                Name= “Address2”
            }
        }
    }
}

The above example assumes that you create classes for Customer and Address.

Also, note that you don’t need to use strings for the all of the properties. Numeric properties can be int or other numeric types. Dates and times can be DateTime, which will be serialized and deserialized as ISO8601 strings automatically.

I am wondering why do we need classes such as Customer and address if I am using class.

@rkbnair

Normally when working with data in Couchbase, having consistent POCOs for serialization and deserialization is important. However, it isn’t a requirement. The key is that you need to build an in-memory object graph in C# that can be serialized by Newtonsoft.Json in a JSON object. So you can’t just use raw JSON in your C# code (unless you do it as one big string). But you could use anonymous types or dynamics instead of well defined POCOs if you wanted to.

LocationAddress is missing a “new” keyword, so it’s syntax issue in your code.