关于 JsonIgnore 问题,
EF T4 模板 中 存在主外键关系
namespace WindowsFormsApplication1
{
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public partial class MaterielProcedures
{
public int Kid { get; set; }
public Nullable<int> OKid { get; set; }
public int Node { get; set; }
public string ProcedureType { get; set; }
public string ProcedureNeed { get; set; }
public string Author { get; set; }
public System.DateTime CreateTime { get; set; }
public Nullable<System.DateTime> StartTime { get; set; }
public Nullable<double> PlanHour { get; set; }
public Nullable<System.DateTime> EndTime { get; set; }
public Nullable<int> PercentAge { get; set; }
public string ProcedureState { get; set; }
public bool IsOut { get; set; }
[JsonIgnore]
public virtual bom_2d bom_2d { get; set; }
}
}
bom_2d 是 MaterielProcedures 的外键 ,用到 json.net 来把 MaterielProcedures 类变成 json 字符串的时候,会因为这个外键报错。
解决 方法是 在 public virtual bom_2d bom_2d { get; set; }上面一行 加上 [JsonIgnore]。
但是 每次重新生成模板 ,都会 重新生成 MaterielProcedures 类。
所以直接找到出问题的源码 ,进行修改,跟踪了 N 多方法 终于
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) { OnSerializing(writer, contract, value); _serializeStack.Add(value); WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty); int initialDepth = writer.Top; for (int index = 0; index < contract.Properties.Count; index++) { JsonProperty property = contract.Properties[index]; try { object memberValue; JsonContract memberContract; if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue)) continue; #region add by 253 if (!memberContract.CreatedType.BaseType.FullName.Contains("System.")) continue; #endregion property.WritePropertyName(writer); SerializeValue(writer, memberValue, memberContract, property, contract, member); } catch (Exception ex) { if (IsErrorHandled(value, contract, property.PropertyName, null, writer.ContainerPath, ex)) HandleError(writer, initialDepth); else throw; } } if (contract.ExtensionDataGetter != null) { IEnumerable<KeyValuePair<object, object>> extensionData = contract.ExtensionDataGetter(value); if (extensionData != null) { foreach (KeyValuePair<object, object> e in extensionData) { JsonContract keyContract = GetContractSafe(e.Key); JsonContract valueContract = GetContractSafe(e.Value); bool escape; string propertyName = GetPropertyName(writer, e.Key, keyContract, out escape); if (ShouldWriteReference(e.Value, null, valueContract, contract, member)) { writer.WritePropertyName(propertyName); WriteReference(writer, e.Value); } else { if (!CheckForCircularReference(writer, e.Value, null, valueContract, contract, member)) continue; writer.WritePropertyName(propertyName); SerializeValue(writer, e.Value, valueContract, null, contract, member); } } } } writer.WriteEndObject(); _serializeStack.RemoveAt(_serializeStack.Count - 1); OnSerialized(writer, contract, value); }
搞定啦!就是 除了System 下面的数据类型,全部 continue!
json.net 比jsonIgnore 更好的方法 修改源码