.NET SDK Bucket.Get

I am facing an issue with the Bucket.Get<type>(docId) API.

I inserted a document into the couchbase as a json string. Code snippet below -

Bucket.Insert(new Document<string>()
Id = docId,
Content = jsonObject.ToString(Formatting.Indented)

After that I try to get the document back from the couchbase using my entity Type which is exactly the deserialized version of the json string that I saved. The statement is as follows


I get an error - "“Unable to cast object of type ‘System.String’ to type ‘Entity.Customer’”.

Is this not suppose to work? Why does couchbase wants to return me a System.String type when I ask for deserialized Customer object.

Please help!

@kris_smarty -

When you inserted the document, you gave it a type: string. The SDK will use this (the Type) as a hint to store transcoding information about how to handle the conversion of the data when it is retrieved from Couchbase.

When you try retrieve the document and you specify a Type of “Customer”, the transcoder will use the information stored on the write to determine how to handle transcoding. In this case the transcoded Type and the application defined type do not match, so the conversion fails.

So, the easy way around this is to specify the type “Customer” when you do the store; this will tell the SDK to provide the correct transcoding information (in this case it will store it as a JSON type).