1

I have a model created in EF core 3.0, using database first approach.

public partial class Study
    {
        public int StudyId { get; set; }
        public int SampleId { get; set; }
        public byte StudyNumber { get; set; }
        public int StudyTypeId { get; set; }
        public int? PhaseId { get; set; }
        public String StudyData { get; set; }
    }

The attribute StudyData maps to a SQL server nvarchar field that holds a JSON document.

This is part of a asp.net core web api and the controller returns JSON.

services.AddControllers().AddNewtonsoftJson(options =>
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                );

My problem is that the controller now serialise twice the StudyData and adds slashes at the original quotes. I am not sure how to avoid this behaviour.

HarryKontop
  • 33
  • 2
  • 6
  • 1
    Appy `[JsonConverter(typeof(RawConverter))]` to `public String StudyData`, where `RawConverter` comes from [this answer](https://stackoverflow.com/a/40539360/3744182) to [How can I serialize and deserialize a type with a string member that contains “raw” JSON, without escaping the JSON in the process](https://stackoverflow.com/q/40529125/3744182). Or do you have some problem applying converters to your `Study` type? – dbc Jun 19 '20 at 19:29
  • Please let us know whether the `[JsonConverter(typeof(RawConverter))]` works for you. – dbc Jun 22 '20 at 23:16
  • 1
    Yeah that did the trick! The `[JsonConverter(typeof(RawConverter))]` serialises the JSON data from the database field properly. – HarryKontop Jun 23 '20 at 07:39

0 Answers0