Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to include related entity? #5752


User avatar
0
luqc1985 created
public class Form : FullAuditedEntity
{
}
[Table("Flows")]
public class FlowUnit : FullAuditedEntity
{
    public virtual long FormId { get; set; }

    [ForeignKey("FormId")]
    public virtual FormUnit FlowForm { get; set; }
}

I have such two tables, there is a primary foreign key relationship, after the data is migrated, when using Repository.GetAsync query, flowUnit.FlowForm is null, I am using EntityFrameworkCore.SqlServer, the previous EntityFramework version does not seem to Question, how can I make the flowUnit.FlowForm have a corresponding value? Can anyone help me for this ? <span class="colour" style="color: rgb(0, 0, 0);">Thanks in advance for any advice given.</span>


4 Answer(s)
  • User Avatar
    1
    aaron created
    Support Team
    var flowUnit = await flowUnitRepository.GetAll()
        .Include(fu => fu.FlowForm)
        .FirstAsync(fu => fu.Id == input.Id.Value);
    
  • User Avatar
    0
    luqc1985 created
    public class Form : FullAuditedEntity
    {
        public virtual List<FlowUnit> FlowUnitList { get; set; }
    }
    

    If I add a navigation property to the Form, FlowUnitList, how should the query be written to get the list?

  • User Avatar
    0
    aaron created
    Support Team
    var flowUnit = await flowUnitRepository.GetAll()
        .Include(fu => fu.FlowForm)
            .ThenInclude(ff => ff.FlowUnitList)
        .FirstAsync(fu => fu.Id == input.Id.Value);
    
  • User Avatar
    0
    luqc1985 created

    Thank you, I already understand the logic inside.