JSON Column: Index was out of range

74 Views Asked by At

I have a problem when doing a JSON COLUMN with EF. What I tried to achieve is JobDetails column in my SQL Server to store a bunch of JSON with dynamic of field, but those field later will be filter a column Team so that the deserializer know what to do. Below are my line of code that I've been tried.

DataContext.cs

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<JobHistory>()
        .OwnsOne(b => b.JobDetails, o =>
         {
           o.ToJson();
         });

    OnModelCreatingPartial(modelBuilder);
}

JobHistory.cs

public partial class JobHistory
{
    [Key]
    public int JobId { get; set; }
    public string? Team { get; set; }
    public string? Name { get; set; }
    public JobDetails? JobDetails { get; set; }
    public int? IsActive { get; set; }
}

public class JobDetails
{
    public List<TeamA>? TeamA { get; set; }
    public List<TeamB>? TeamB { get; set; }
}

JobService

public async Task AddJob(JobHistory jobHistory)
{
   try
   {
      _data.JobHistory.Add(jobHistory);
      await _data.SaveChangesAsync();
   }
   catch(Exception ex)
   {

   }
}
public async Task<List<JobHistory>> GetAllJob(string team)
{
   try
   {
      var jobs = await _data.JobHistory
           .Where(j => j.Team == team && j.IsActive == 1)
           .ToListAsync();
           return jobs;
   }
   catch (Exception ex)
   {
      return new List<JobHistory>();
   }
}

From those approach I keep getting an error for both saving a data and tried to retrieve it

{"Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')"}

0

There are 0 best solutions below