我是Entity Framework的新手,所以如果这不是一个有效的问题,我会道歉 .

几个星期前我开始使用.NET Web Api,当我不得不处理更复杂的实体时,我遇到了json序列化的问题 . 经过一些研究,我找到了不同的方法,例如:

的Global.asax.cs

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

要么

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

[JsonIgnore]

第一个对我不起作用,所以我选择了[JsonIgnore]属性 .

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

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

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

谢谢

编辑:

序列化StackTrace:

{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Self referencing loop detected with type 'Pathfinders.Data.Natural_Person'. Path '[0].Natural_Persons[0].Gender.Natural_Persons'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":“at 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值,JsonArrayContract Contract ,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(Json)编写器编写器,Object值,JsonObjectContract Contract ,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,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在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter) writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IWrappedCollection值,JsonArrayC) ontract contract,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,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构件,JsonContainerContract containerContract,JsonProperty containerProperty个)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter作家,IWrappedCollection值,JsonArrayContract Contract ,JsonProperty构件,J在Newtonsoft.Json.Serialization中,在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n的sonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n . JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object值)\ r \ n在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)“}}