0

我是实体框架的新手,所以如果这不是一个有效的问题,我深表歉意。

几周前我开始使用 .NET Web Api,当我必须处理更复杂的实体时,我遇到了 json 序列化的问题。经过一番研究,我发现了不同的方法,例如:

全球.asax.cs

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling= Newtonsoft.Json.ReferenceLoopHandling.Ignore;

或者

对于我不想序列化的每个属性(在 context.designer.cs 文件中),添加:

[JsonIgnore]

第一个对我不起作用,所以我使用了 [JsonIgnore] 属性。

不幸的是,每隔一段时间我必须对实体框架上下文设计进行更改,一旦我保存这些更改,我就会丢失我添加到我不想序列化的属性的属性。

那么问题是:有没有办法将 JsonIgnore 永久设置为特定属性?或者防止实体框架覆盖特定属性或类的更改的方法?

甚至...... JsonIgnore 是最好的方法吗?

谢谢

编辑:

序列化堆栈跟踪:

{"Message":"发生错误。","ExceptionMessage":"'ObjectContent`1' 类型未能序列化内容类型 'application/json; charset=utf-8' 的响应正文。","ExceptionType ":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"发生错误。","ExceptionMessage":"检测到“Pathfinders.Data.Natural_Person”类型的自引用循环。路径 '[0].Natural_Persons[0].Gender.Natural_Persons'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference (JsonWriter writer, Object值,JsonProperty 属性,JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json .Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 编写器,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter 编写器,对象值,JsonObjectContract 合同,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization。JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member,JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization。 JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter。SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n 在系统。 Net.Http.Formatting.JsonMediaTypeFormatter.<>c_JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c_JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c_序列化(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c_序列化(JsonWriter jsonWriter, Object value)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c_DisplayClassd.b _c()\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"}}

4

0 回答 0