I'm getting this error when I try to GET an item from Mongo right after starting my API:
当我尝试在启动API后立即从Mongo获取项目时,收到以下错误:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.AggregateException: One or more errors occurred. (An error occurred while deserializing the Fields property of class divitiae_api.Models.App: Element 'AppRelationId' does not match any field or property of class divitiae_api.Models.FieldStructure.)
The item that I'm trying to get (called App) has the next structure:
我正在尝试获取的项(称为App)具有下一个结构:
public class App
{
[BsonId]
public ObjectId Id { get; set; }
public string StringId { get; set; } = string.Empty;
public string AppName { get; set; } = string.Empty;
public List<FieldStructure> Fields { get; set; } = new List<FieldStructure>();
public List<ObjectId> Items { get; set; } = new List<ObjectId>();
}
And the property that "fails" to be serialized is this one:
而“无法”序列化的属性是:
public class FieldStructure
{
public FieldStructure(string name, string type)
{
Name = name;
Type = type;
NameAsProperty = name.ToLower().Replace(" ", "_");
}
public FieldStructure() { }
public string Name { get; set; } = string.Empty;
public string NameAsProperty { get; set; } = string.Empty;
public string Type { get; set; } = string.Empty;
}
public class FieldStructureRelation : FieldStructure
{
public FieldStructureRelation(FieldStructure fieldStructure, string appRelationId)
: base(fieldStructure.Name, fieldStructure.Type)
{
this.AppRelationId = appRelationId;
}
public FieldStructureRelation(string name, string type, string appRelationId)
: base(name, type)
{
this.AppRelationId = appRelationId;
}
public FieldStructureRelation() { }
public string AppRelationId { get; set; } = string.Empty;
}
The App's "fields" property is basically a list of FieldStructure objects that can be both FieldStructures and FieldStructureRelations. I get no errors when inserting these type of lists as the app detects them as compatible for that same list.
App的“field”属性基本上是FieldStructure对象的列表,这些对象既可以是FieldStructures也可以是FieldStrucreRelationship。在插入这些类型的列表时,我没有收到任何错误,因为应用程序检测到它们与同一列表兼容。
The weird thing is that if I start the api and try to get one of these "App" objects, I get the error that I first showed saying that "FieldStructure" has no "AppRelationId" property (in this case, the system should detect it as a "FieldStructureRelation", not the other one). But, if I create an App with a mix of Fields being "FieldStructure" and "FieldStructureRelation", then every single GET request after this action is succesful.
奇怪的是,如果我启动api并尝试获取其中一个“App”对象,我会得到我第一次显示的错误,说“FieldStructure”没有“AppsourceId”属性(在这种情况下,系统应该将其检测为“FieldStructureRelation”,而不是另一个)。但是,如果我创建一个应用程序,其中混合了“FieldStructure”和“FieldStructureRelation”字段,那么在此操作之后的每个GET请求都是成功的。
I don't understand what is going on here, so any help is very much appreciated!
我不知道这里发生了什么,所以任何帮助都是非常感激的!
更多回答
我是一名优秀的程序员,十分优秀!