1

Before you mark this as a duplicate of This one know that I've used that.

I want to connect to a DB using EF core.

My desired process:

  1. I receive information from a JSON post
  2. deserialize it to a set of objects based on the structure of the JSON.
  3. (previously stored a bunch of this in the DB for testing)
  4. Join two different C# objects into a single table in the DB
  5. Retrieve two different C# objects from a single table from the DB 4 and 5 are where I'm stuck

Json:

{
  "jobId": "ad86dbd6-ccd7-4ef4-b317-c990a8eaa3a3",
  "status": "COMPLETED",
  "report": {
     "processedCount": 18,
     "importedCount": 0,
     "failedCount": 18,
     "errors": [
        {
          "Row": 2,
          "_id": "qaautoAssetValidation01",
          "_import action": "C",
          "fieldName": "qaauto attribute date01",
          "errorCode": "10011",
          "Error": "Mandatory attribute qaauto attribute date01 not supplied"
        }
    }
}

I have created the following objects to correlate to the JSON.

    [Table("ImportJobStatusReport")]
    public class ApiResponseModel
    {
        [JsonProperty(PropertyName = "jobId")]
        public Guid JobId { get; set; }

        [JsonProperty(PropertyName = "status")]
        public string JobStatus { get; set; }

        [JsonProperty(PropertyName = "report")]
        public StatusReport Report { get; set; }

    }


    [Table("ImportJobStatusReport")]
    public class StatusReport
    {
        //Only added this ID to help with the EF mapping below
        public Guid JobId { get; set; }

        [JsonProperty(PropertyName = "processedCount")]
        public int ProcessedCount { get; set; }

        [JsonProperty(PropertyName = "importedCount")]
        public int ImportedCount { get; set; }

        [JsonProperty(PropertyName = "failedCount")]
        public int FailedCount { get; set; }

        [JsonProperty(PropertyName = "errors")]
        public List<ProductImportErrorModel> Errors { get; set; }

        //Only added this for mapping below
        public ApiResponseModel Response { get; set; }

    }

The Database has a single object for these two since I don't really need two tables to represent a single status update.

ImportJobStatusReport table

JobId    JobStatus    ProcessedCount    ImportedCount    FailedCount
(guid)   COMPLETED    18                0                18

My mapping:

modelBuilder.Entity<ApiResponseModel>()
.HasOne(e => e.Report)
.WithOne(e => e.response)
.HasForeignKey<ImportJobStatusReportModel>(e => e.JobId);
 modelBuilder.Entity<ApiResponseModel>().ToTable("ImportJobStatusReport");
 modelBuilder.Entity<ImportJobStatusReportModel>().ToTable("ImportJobStatusReport");

Now for some reason when I try to retrieve an ApiResponseModel from the DB I WANT and EXPECT it to have all the values in the database and give me back:

ApiResponseModel
  -JobId
  -JobStatus
   Report
     -ProcessedCount
     -ImportedCount
     -FailedCount 
     -Errors (probably null for now)

Instead what I get back is:

ApiResponseModel
  -JobId
  -JobStatus
   null

I have tried all manner of different mappings to try and get EF to recognize that both objects are tied to a single line in the DB and populate both objects when retrieving them from the DB but I must me missing something fundamental.

leigero
  • 3,233
  • 12
  • 42
  • 63
  • 1
    You probably need to use [Table Splitting](https://learn.microsoft.com/en-us/ef/core/modeling/table-splitting) and derive ApiResponseModel and StatusReport from a common base class containing JobId and JobStatus. – AlwaysLearning Aug 29 '19 at 21:57
  • 3
    The mapping is correct. The issue has nothing to do with table splitting, but [Loading Related Data](https://learn.microsoft.com/en-us/ef/core/querying/related-data), for instance eager loading `.Include(e => e.Report)` – Ivan Stoev Aug 29 '19 at 22:11
  • 1
    @IvanStoev Yep. 5 hours of digging and I didn't know what I was looking for. That was precisely it. thanks. – leigero Aug 29 '19 at 23:02

0 Answers0