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
};
}