C# 如何使用表拆分将多个对象映射到EF Core中的单个表?

C# 如何使用表拆分将多个对象映射到EF Core中的单个表?,c#,entity-framework,entity-framework-core,mapping,table-splitting,C#,Entity Framework,Entity Framework Core,Mapping,Table Splitting,在你把它标记为复制品之前,我知道我用过它 我想使用EF core连接到DB 我想要的过程: 我从一个JSON帖子收到信息 根据JSON的结构将其反序列化为一组对象 (以前在DB中存储了一组用于测试) 将两个不同的C#对象连接到数据库中的一个表中 从数据库的单个表中检索两个不同的C#对象 4和5是我被卡住的地方 Json: 我创建了以下与JSON相关的对象 [Table("ImportJobStatusReport")] public class ApiResponseModel

在你把它标记为复制品之前,我知道我用过它

我想使用EF core连接到DB

我想要的过程:

  • 我从一个JSON帖子收到信息
  • 根据JSON的结构将其反序列化为一组对象
  • (以前在DB中存储了一组用于测试)
  • 将两个不同的C#对象连接到数据库中的一个表中
  • 从数据库的单个表中检索两个不同的C#对象 4和5是我被卡住的地方
  • Json:

    我创建了以下与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; }
    
        }
    
    我的映射:

    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");
    
    相反,我得到的是:

    ApiResponseModel
      -JobId
      -JobStatus
       null
    

    我尝试了各种不同的映射,试图让EF识别出这两个对象都绑定到DB中的一行,并在从DB中检索它们时填充这两个对象,但我必须指出我遗漏了一些基本信息。

    您可能需要从包含JobId和JobStatus。映射正确。这个问题与表拆分无关,但是,例如,急切加载
    .Include(e=>e.Report)
    @IvanStoev-Yep。5个小时的挖掘,我不知道我在找什么。正是这样。谢谢。您可能需要使用包含JobId和JobStatus的公共基类并从中派生ApiResponseModel和StatusReport。映射是正确的。这个问题与表拆分无关,但是,例如,急切加载
    .Include(e=>e.Report)
    @IvanStoev-Yep。5个小时的挖掘,我不知道我在找什么。正是这样。谢谢
    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");
    
    ApiResponseModel
      -JobId
      -JobStatus
       Report
         -ProcessedCount
         -ImportedCount
         -FailedCount 
         -Errors (probably null for now)
    
    ApiResponseModel
      -JobId
      -JobStatus
       null