【发布时间】:2021-08-11 03:24:22
【问题描述】:
我有以下实体类:
public class FacebookComment : BaseEntity
{
[BsonId(IdGenerator = typeof(ObjectIdGenerator))]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
[JsonProperty("_id")]
public ObjectId Id { get; set; }
public int? OriginalId { get; set; }
public DateTime Date { get; set; }
public string Message { get; set; }
public string Sentiment { get; set; }
public string Author { get; set; }
}
当此对象序列化为 JSON 时,我希望将 Id 字段写为“_id”:{...}。 AFAIK,我应该只需将所需的属性名传递给 JsonProperty 属性;我应该很高兴。但是,当我调用 JsonConvert.SerializeObject;它似乎忽略了我的属性并呈现它:
{
Author: "Author name",
Date: "/Date(1321419600000-0500)/",
DateCreated: "/Date(1323294923176-0500)/",
Id: {
CreationTime: "/Date(0)/",
Increment: 0,
Machine: 0,
Pid: 0,
Timestamp: 0
},
Message: "i like stuff",
OriginalId: null,
Sentiment: "Positive"
}
如您所见,正在使用错误的字段名称呈现 Id 字段。
有什么想法吗?在这个问题上工作了一个小时;无法弄清楚为什么序列化程序似乎忽略了我的 JsonProperty。
非常感谢任何建设性的意见。
【问题讨论】:
-
那么 BaseEntity 也有 and Id 属性?
-
不,BaseEntity 只有一个 DateCreated 字段
-
你有想过这个吗?我很确定我的答案(我花了两个小时!)可能是发生了什么