Created
March 6, 2018 10:45
-
-
Save Segment0895/ee04dce8d0bec703d731396e96074fc6 to your computer and use it in GitHub Desktop.
phonyDTOfromEF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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