【问题标题】:Exception inserting List<T> in RavenDB在 RavenDB 中插入 List<T> 时出现异常
【发布时间】:2012-09-07 07:11:41
【问题描述】:
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.

我确实尝试将列表转换为数组...即使感觉不对...也失败了...此外,为什么我必须使用 RavenJObjectRavenJArray 而不是Json.net 附带的默认值.. 我猜引擎盖下正在进行一些漂亮的工作..

【问题讨论】:

标签: c# .net ravendb


【解决方案1】:

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

该 api 期望接收一个实体,而不是一个数组或集合。

您可能想利用批处理操作来处理一堆问题的加载:http://ravendb.net/docs/client-api/advanced/databasecommands/batch

【讨论】:

    猜你喜欢
    • 2013-03-17
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多