API Controller returns corrupted data

Viewed 111

I am currently working on a Blazor Webassembly App and am struggling with my API-Controller. In the get method of my API controller (see below), I am trying to join the objects using Linq.

When I try to fetch the data with the API call, in exactly every second object the properties TypeId and Type (sometimes even the Status) are null. TypeId can be null, but in this query there is a TypeId in every object.

On calling the API, Visual Studio shows me the associated SQL request in the output window. When I copy/paste this request to SSMS, I get the correct data with joins, as expected.

If I comment the two includes in the get method, all objects have a TypeId. The method to fetch single objects works fine.

Models:

    public partial class Surety
    {
        public int Number { get; set; }
        public int? TypeId { get; set; }
        public int StatusId { get; set; }
        ...
        public SuretyStatus Status { get; set; } = null!;
        public SuretyType? Type { get; set; }
    }

    public partial class SuretyType
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
    }

    public partial class SuretyStatus
    {
        public int Id { get; set; } 
        public string? Name { get; set; }
    }

Model-Creating in Db-Context:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Surety>(entity =>
            {
                entity.HasKey(e => e.Number);

                entity.Property(e => e.Number).ValueGeneratedNever();

                entity.HasOne(s => s.Status)
                    .WithOne()
                    .HasForeignKey<Surety>(d => d.StatusId)
                    .HasConstraintName("FK_Sureties_SuretyStatus");

                entity.HasOne(d => d.Type)
                    .WithOne()
                    .HasForeignKey<Surety>(d => d.TypeId)
                    .HasConstraintName("FK_Sureties_SuretyType");
            });

            modelBuilder.Entity<SuretyStatus>(entity =>
            {
                entity.ToTable("SuretyStatus");

                entity.Property(e => e.Id).ValueGeneratedNever();

                entity.Property(e => e.Name).HasMaxLength(30);
            });

            modelBuilder.Entity<SuretyType>(entity =>
            {
                entity.ToTable("SuretyType");

                entity.Property(e => e.Id).ValueGeneratedNever();

                entity.Property(e => e.Name).HasMaxLength(30);
            });

            OnModelCreatingPartial(modelBuilder);
        }

Api-Controller:

        [HttpGet]
        public async Task<ActionResult<IEnumerable<Surety>>> GetSureties()
        {
            if (_context.Sureties == null)
                return NotFound();

            return await _context.Sureties
                .Include(s => s.Type)
                .Include(s => s.Status)
                .ToListAsync();
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<Surety>> GetSuretyById(int id)
        {
            if (_context.Sureties == null)
                return NotFound();

            var surety = await _context.Sureties
                .Include(s => s.Type)
                .Include(s => s.Status)
                .SingleOrDefaultAsync(s => s.Number == id);

            if (surety == null)
                return NotFound();

            return surety;
        }

Extract from JSON of the API-response.

   {
      "number":1001,
      "typeId":null,
      "statusId":3,
      "status":null,
      "type":null
   },
   {
      "number":1002,
      "status":{
         "id":3,
         "name":"name"
      },
      "type":{
         "id":1,
         "name":"name",
      }
   },
   {
      "number":1003,
      "typeId":null,
      "statusId":3,
      "status":{
         "id":3,
         "name":"name"
      },
      "type":null
   },
   {
      "number":1004,
      "typeId":1,
      "statusId":3,
      "status":{
         "id":3,
         "name":"name"
      },
      "type":{
         "id":1,
         "name":"name",
      }
   },
   {
      "number":1005,
      "typeId":null,
      "statusId":3,
      "status":{
         "id":3,
         "name":"name"
      },
      "type":null
   },
   {
      "number":1006,
      "typeId":1,
      "statusId":3,
      "status":{
         "id":3,
         "name":"name"
      },
      "type":{
         "id":1,
         "name":"name",
      }
   },
   {
      "number":1007,
      "typeId":null,
      "statusId":3,
      "status":{
         "id":3,
         "name":"name"
      },
      "type":null
   },

Does anyone know what I'm doing wrong and why the API response is missing data in every other record?

0 Answers
Related