What is the correct way to tell WebApi2 and EF6 to not serialize a field to Json?

163 Views Asked by At

I have an Entity Framework Table per Type hierarchy like this:

public class WorkItem
{
    public int WorkItemId {get;set;}
}

public class CancelingWorkItem : WorkItem
{
    public int WorkItemIdToCancel {get;set;}

    [ForeignKey("WorkItemIdToCancel")]
    public virtual WorkItem WorkItemToCancel {get;set}
}

public class SomeOtherWorkItem : WorkItem
{
    // more fields...
}

When I return a list of all WorkItems in the database as Json, any serialized CancelingWorkItem will contain the full definition of the WorkItemToCancel field. I could just ignore this field with JsonIgnore, but I was wondering if there was a different/better way of doing this. My repository project doesn't yet rely on Json.Net, so if I can instead tell the controller not to serialize that field, that might be a better solution.

1

There are 1 best solutions below

1
On

You can use the IgnoreDataMemberAttribute attribute - It's not from an external library like JsonIgnore and I think that the default serializer and Json.NET will both recognise this attribute.