RavenDB:如何防止将 $type 保存到动态类型 属性?
RavenDB: How to prevent $type being saved to a dynamic typed property?
有什么方法可以防止在我保存动态类型值时添加 $type
属性?
当我保存这个时:
new Activity {
Name = "FormFieldDeleted",
Body = new {
MyDeletedFormField(),
MyCompleteForm()
}
}
我明白了
<>f__AnonymousType1`2[[MyProject.Domains.Forms.Models.FormField, MyProject.Domains.Forms],[MyProject.Domains.Forms.Entities.FormRegistration, MyProject.Domains.Forms]], MyProject.Api.Forms
但是当我尝试获取这个已保存的实体时,它崩溃了,出现以下异常。我知道它缺少项目引用,但我真的不想添加该引用(我不想从控制台应用程序引用 API)。我最好只阻止 $type
属性.
/usr/local/share/dotnet/dotnet path/MyProject/MyProject/src/MyProject.Tasks.MapActivities/bin/Debug/netcoreapp3.1/MyProject.Tasks.MapActivities.dll
Unhandled exception. System.InvalidOperationException: Could not convert document 31317d58-db9e-4f60-8dee-b8593f3e06c0 to entity of type MyProject.Domains.Core.Entities.Activity
---> Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON '<>f__AnonymousType1`2[[MyProject.Domains.Forms.Models.FormField, MyProject.Domains.Forms],[MyProject.Domains.Forms.Entities.FormRegistration, MyProject.Domains.Forms]], MyProject.Api.Forms'. Path 'Body.$type'.
---> Newtonsoft.Json.JsonSerializationException: Could not load assembly 'MyProject.Api.Forms'
....
是的,有办法。
您可以使用以下代码自定义序列化的工作方式:
store.Conventions.CustomizeJsonSerializer = serializer =>
{
serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
};
举个例子,看看这里的代码:https://dotnetfiddle.net/voJ7US
如果您在 dotnetfiddle 处执行代码,您可以在此处查看结果:http://live-test.ravendb.net/studio/index.html#databases/documents?collection=Activities&database=UniqueTestDB
对于 RavenDB 5 及更高版本,它发生了一些变化。
var store = DocumentStore
{
Urls = new[] { "your-endpoint" },
Conventions = new DocumentConventions
{
Serialization = new NewtonsoftJsonSerializationConventions
{
CustomizeJsonSerializer = serializer =>
{
serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
}
}
}
}.Initialize();
有关详细信息,请参阅 https://ravendb.net/docs/article-page/5.0/file-header/migration/client-api/conventions。
有什么方法可以防止在我保存动态类型值时添加 $type
属性?
当我保存这个时:
new Activity {
Name = "FormFieldDeleted",
Body = new {
MyDeletedFormField(),
MyCompleteForm()
}
}
我明白了
<>f__AnonymousType1`2[[MyProject.Domains.Forms.Models.FormField, MyProject.Domains.Forms],[MyProject.Domains.Forms.Entities.FormRegistration, MyProject.Domains.Forms]], MyProject.Api.Forms
但是当我尝试获取这个已保存的实体时,它崩溃了,出现以下异常。我知道它缺少项目引用,但我真的不想添加该引用(我不想从控制台应用程序引用 API)。我最好只阻止 $type
属性.
/usr/local/share/dotnet/dotnet path/MyProject/MyProject/src/MyProject.Tasks.MapActivities/bin/Debug/netcoreapp3.1/MyProject.Tasks.MapActivities.dll
Unhandled exception. System.InvalidOperationException: Could not convert document 31317d58-db9e-4f60-8dee-b8593f3e06c0 to entity of type MyProject.Domains.Core.Entities.Activity
---> Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON '<>f__AnonymousType1`2[[MyProject.Domains.Forms.Models.FormField, MyProject.Domains.Forms],[MyProject.Domains.Forms.Entities.FormRegistration, MyProject.Domains.Forms]], MyProject.Api.Forms'. Path 'Body.$type'.
---> Newtonsoft.Json.JsonSerializationException: Could not load assembly 'MyProject.Api.Forms'
....
是的,有办法。
您可以使用以下代码自定义序列化的工作方式:
store.Conventions.CustomizeJsonSerializer = serializer =>
{
serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
};
举个例子,看看这里的代码:https://dotnetfiddle.net/voJ7US
如果您在 dotnetfiddle 处执行代码,您可以在此处查看结果:http://live-test.ravendb.net/studio/index.html#databases/documents?collection=Activities&database=UniqueTestDB
对于 RavenDB 5 及更高版本,它发生了一些变化。
var store = DocumentStore
{
Urls = new[] { "your-endpoint" },
Conventions = new DocumentConventions
{
Serialization = new NewtonsoftJsonSerializationConventions
{
CustomizeJsonSerializer = serializer =>
{
serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
}
}
}
}.Initialize();
有关详细信息,请参阅 https://ravendb.net/docs/article-page/5.0/file-header/migration/client-api/conventions。