gpt4 book ai didi

c# - RavenDB:如何防止 $type 被保存到动态类型属性?

转载 作者:行者123 更新时间:2023-12-03 21:14:48 24 4
gpt4 key购买 nike

有没有办法防止$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属性(property)。
/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

关于c# - RavenDB:如何防止 $type 被保存到动态类型属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61526001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com