1

I am using Entity Framework 6 to build a model of DB. I've got also web service built on Web Api 2 with JSON..

My main partial class definition looks like that: (Generated from EF (first code from DB) so I don't want to store here too much changes )

public partial class Animal
{
    [Key]
    [Column(Order = 0)]
    public long AnimalId { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long SpeciesId { get; set; }

    [Key]
    [Column(Order = 2)]
    public string AnimalName{ get; set; }

    public string AnimalDescription{ get; set; }

    public List<AnimalTags> AnimalTags= new List<AnimalTags>();
}

and I've got also another one partial class with some additional properties, which I don't want to show through Web Api.

public partial class Animal
{
    [JsonIgnore]
    public bool IsNew { get; set; } = false;
    [JsonIgnore]
    public bool IsChanged { get; set; } = false;
}

That's my AnimalController :

public IHttpActionResult GetAnimals(long id)
{
    Animal animal = (from an in db.Animal
                       where a.AnimalId == id
                       select a).FirstOrDefault();

    animal.AnimalsTags= db.AnimalTags.Where(at=> at.AnimalId == animal.AnimalId).ToList();

    if (animal == null)
    {
        return NotFound();
    }

    return Ok(animal);
}

I've checked all suggested solution on SO and MSDN but those attributes are not working. Maybe I made a mistake somewhere else?

Thanks for any help.

Edit: DB Model is in other project and WebService is in another one, so I am linking DB Model to WebService through Reference.

How my register class looks like with Newtonsoft.Json :

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
                name: "DataApi",
                routeTemplate: "api/{controller}/{Id}",
                defaults: new { Id = RouteParameter.Optional }
        );
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        config.Formatters.JsonFormatter.SerializerSettings =
            new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                Formatting = Formatting.Indented
            };
    }
17
  • did you try [NotMapped] ? Commented Dec 20, 2016 at 12:23
  • @MMK : Yes. But without success. Commented Dec 20, 2016 at 12:27
  • [ScriptIgnore(ApplyToOverrides = true)] Commented Dec 20, 2016 at 12:29
  • Where did you find that you should use either [IgnoreDataMember] or [ScriptIgnore]? Neither apply to the default JSON serializer for WebAPI, being Newtonsoft.Json. Commented Dec 20, 2016 at 12:31
  • @CodeCaster: I've thought that I wrote that clearly that I am not using Newtonsoft.Json. Commented Dec 20, 2016 at 12:46

1 Answer 1

1

I think you are looking for [JsonIgnore] for the Json serializer or [IgnoreDataMember] for the default XML serializer.

But I can see in your code that you are using [DataContract], you can definitely do this also:

[DataContract]
public class YourClass
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Prop1 { get; set; }

    //ignored
    public string IgnoredProp { get;set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am not using Json.NET, I am using DataContractJsonSerializer. So I can't us e [JsonIgnore]. And to the second part, as you can see, in partial class I don't have [DataMember] attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.