ashutosh raina
ashutosh raina

Reputation: 9314

Exception inserting List<T> in RavenDB

public class Question
    {
        public int Id { get; set; }
        public int? Score { get; set; }
        public string Title { get; set; }
        public List<string> Tags { get; set; }
        public Owner Owner { get; set; }
        public Uri Link { get; set; }
        public bool IsAnswered { get; set; }
    }

public class Owner
    {
        public int? ID { get; set; }
        public String Name { get; set; }
        public int Reputation { get; set; }
        public Uri Link { get; set; }
    }

public static dynamic CallStackOverFlow()
        {
            var client = new RestClient("https://api.stackexchange.com/2.1");
            var request = new RestRequest("/search/advanced", Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.AddParameter("site", "stackoverflow");
            request.AddParameter("tagged", "C#");
            request.AddParameter("pagesize", "1");
            var response = client.Execute(request);
            var content = response.Content; // raw content as string
            dynamic deserialised = JsonConvert.DeserializeObject(content);
            return deserialised;
        }

//go call stackoverflow
            var d = StackOverflow.CallStackOverFlow();
            var questions = new List<Question>();
            foreach (var q in d.items)
            {
                Console.WriteLine(q);
                var question = new Question
                {
                    Id = q.question_id,
                    IsAnswered = q.is_answered,
                    Link = new Uri(q.link == null ? "" : (string)q.link),
                    Owner = new Owner
                    {
                        ID = q.owner.user_id,
                        Link = new Uri(q.owner.link == null ? "" : (string)q.owner.link),
                        Name = q.owner.display_name,
                        Reputation = q.owner.reputation
                    },
                    Tags = (q.tags as JArray).Values().Select(v => v.ToString()).ToList(),
                    Score = q.score,
                    Title = q.title
                };

                questions.Add(question);
            }
            using (IDocumentStore documentStore = new DocumentStore() { Url = "http://localhost:8080", DefaultDatabase = "StackOverflow" })
            {
                documentStore.Initialize();
                using (IDocumentSession session = documentStore.OpenSession())
                {
                    session.Store(questions);
                    session.SaveChanges();
                }
            } 

I am just starting out with RavenDb so forgive me if this sounds a bit stupid. I went through the client api...can't figure out why am I getting the following exception..

Object serialized to Array. RavenJObject instance expected.

I did try to cast a the list to an array ...even though it did not feel right.. that failed as well.. Further , Why do I have to dela with RavenJObject and RavenJArray instead of the default that comes with Json.net.. I am guessing there is nifty work going on under the hood..

Upvotes: 2

Views: 1836

Answers (1)

Alonso Robles
Alonso Robles

Reputation: 401

The exception is occurring because you are attempting to store an array of questions when you call: session.Store(questions);

The api is expecting to receive an entity to persist not an array or collection.

You may want to make use of batch operations to handle the loading of a bunch of questions: http://ravendb.net/docs/client-api/advanced/databasecommands/batch

Upvotes: 6

Related Questions