Skip to content

Instantly share code, notes, and snippets.

@Segment0895
Created March 6, 2018 10:45
Show Gist options
  • Save Segment0895/ee04dce8d0bec703d731396e96074fc6 to your computer and use it in GitHub Desktop.
Save Segment0895/ee04dce8d0bec703d731396e96074fc6 to your computer and use it in GitHub Desktop.
phonyDTOfromEF
// credit RAM && BrianRogers -- https://stackoverflow.com/questions/26162902/how-can-i-do-json-serializer-ignore-navigation-properties/#32797274
namespace DataModel
{
class CustomResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty prop = base.CreateProperty(member, memberSerialization);
var propInfo = member as PropertyInfo;
if (propInfo != null)
{
if (propInfo.GetMethod.IsVirtual && !propInfo.GetMethod.IsFinal)
{
prop.ShouldSerialize = obj => false;
}
}
return prop;
}
}
}
// credit RAM && BrianRogers -- https://stackoverflow.com/questions/26162902/how-can-i-do-json-serializer-ignore-navigation-properties/#32797274
namespace DataModel
{
public partial class DbContext2 : DbContext
{
JsonSerializerSettings JsonSettings;
public DbContext2() : base()
{
JsonSettings = new JsonSerializerSettings()
{
ContractResolver = new CustomResolver(),
PreserveReferencesHandling = PreserveReferencesHandling.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment