在RavenDB中插入List <t>的异常</t>

时间:2012-09-06 19:09:08

标签: c# .net 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();
                }
            } 

我刚刚开始使用RavenDb,请原谅我,如果这听起来有点愚蠢。 我经历了客户端API ...无法弄清楚为什么我得到以下异常..

Object serialized to Array. RavenJObject instance expected.

我确实尝试将列表强制转换为数组...即使它感觉不对......也失败了..此外,为什么我必须使用RavenJObject和{{1而不是RavenJArray附带的默认值。我猜测引擎盖下有漂亮的工作......

1 个答案:

答案 0 :(得分:6)

发生异常是因为您在致电时试图存储一系列问题:session.Store(questions);

api期望接收一个实体来坚持不是数组或集合。

您可能希望使用批处理操作来处理大量问题的加载:http://ravendb.net/docs/client-api/advanced/databasecommands/batch