我需要做什么才能在 RavenDB 中存储对象? [英] What do I need to do to store objects in RavenDB?
本文介绍了我需要做什么才能在 RavenDB 中存储对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 ravendb 来序列化一个对象并通过 mstest 对其进行测试.
I'm using ravendb to serialize an object and test it through mstest.
我得到这个结果:System.ArgumentException:对象序列化为字符串.需要 RavenJObject 实例.
这是我的代码
public class Store
{
private static IDocumentStore store = createStore();
private static EmbeddableDocumentStore createStore()
{
var returnStore = new EmbeddableDocumentStore();
returnStore.DataDirectory = @"./PersistedData";
returnStore.Initialize();
return returnStore;
}
public static void Write(string value)
{
using (var session = store.OpenSession())
{
session.Store(value);
session.SaveChanges();
}
}
}
似乎根本原因在于 RavenJObject 的工作方式,因为这会引发相同的错误:
It seems the root cause is in how RavenJObject works as this throws the same error:
RavenJObject storeMe = RavenJObject.FromObject("errors", new JsonSerializer());
如何在 RavenDB 中进行自定义序列化?
How do I do custom serialization in RavenDB?
推荐答案
要对未编写的类进行自定义序列化(因此无法设置属性),请实现 Newtonsoft.Json.JsonConverter
To do custom serialization with a class you didn't write (so you can't attribute) implement Newtonsoft.Json.JsonConverter
然后像这样注册:
using (var session = store.OpenSession())
{
session.Advanced.Conventions.CustomizeJsonSerializer = serializer => serializer.Converters.Add(MailAddressJsonConverter.Instance);
session.Store(mailAddress);
session.SaveChanges();
}
这篇关于我需要做什么才能在 RavenDB 中存储对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文