简体   繁体   English

如何让 EF Core 使用存储库自动填充实体的子 object 中的值?

[英]How do I get EF Core to automatically populate values in an entity's child object using a repository?

How do I get EF Core to automatically populate values in an entity's child object from the look up table using a repository?如何让 EF Core 使用存储库从查找表中自动填充实体的子 object 中的值?

public class JobTask
{
      [Key]
        public int Id { get; set; }

        [Required]
        [StringLength(128)]
        public string Name { get; set; }

        [ForeignKey("FuelType")]
        public int StatusID { get; set; }
        public StatusObj JobStatus { get; set; }
}

 public class StatusObj
    {
        [Key]
        public int Id  { get; set; }

        [Required]
        [StringLength(30)]
        public string Description { get; set; }

    }

To be clear, the status table in the database is just a look up table.需要明确的是,数据库中的状态表只是一个查找表。 The status table has an ID and a Description.状态表有一个 ID 和一个描述。 The jobtask table hold a fk reference to the id in the status look-up table. jobtask 表保存对状态查找表中 id 的 fk 引用。 One JobTask can only have one status at any one time.一个 JobTask 在任何时候只能有一个状态。

But whenever a row for a jobtask is requested, how do I get ef core to automatically populate the Status object on the JobTask object?但是,每当请求作业任务的一行时,我如何让 ef core 自动填充 JobTask object 上的状态 object?

When querying your JobTask from your DatabaseContext you need to include the StatusObj like this:从 DatabaseContext 查询JobTask时,您需要像这样包含 StatusObj:

_context.JobTask.Include(jobTask => jobTask.JobStatus).ToList();

for more details check the docshere有关更多详细信息,请查看此处的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM