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:
- I receive information from a JSON post
- deserialize it to a set of objects based on the structure of the JSON.
- (previously stored a bunch of this in the DB for testing)
- Join two different C# objects into a single table in the DB
- 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.