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

DTO for entity with a one to many relationship #1439


User avatar
0
andry3ag created

public class Dashboard : FullAuditedEntity<int, DashboardModuleUser> { ...

    public virtual ICollection&lt;Widget&gt; Widgets { get; set; }
}

public class Widget : FullAuditedEntity&lt;int, DashboardModuleUser&gt;
{
    [ForeignKey("WidgetTemplateId")]
    public virtual WidgetTemplate WidgetTemplate { get; set; }
    public virtual int WidgetTemplateId { get; set; }

    [ForeignKey("DashboardId")]
    public virtual Dashboard Dashboard { get; set; }
    public virtual int DashboardId { get; set; }
}

So, I have the relationship above established, but when I return it to the frontend as PagedResult, it seems the Widgets in the Dashboard are not eager loading.

How should I setup to ensure my collection of Dashboards will eagerload Widgets and all my Widgets will inturn eager load WidgetTemplate BUT not Dashboard of course, else it will have circular reference issue.

My current Dto for Dashboard are set up as so:

[AutoMapFrom(typeof(Dashboard))]
public class DashboardDto : EntityDto
{
    public string Title { get; set; }

    public DashboardModuleUser User { get; set; }

    public string Content { get; set; }

    public IEnumerable&lt;Widget&gt; Widgets { get; set; }
}

Thank you.


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    First of all, I offer you not to use entities in Dto's. Create Dtos for Widget and DashboardModuleUser (I assume this is an entity as well) and use them in DashboardDto.

    When you get Dashboard data from database, also include Widgets and include WidgetTemplates in your LinQ query, then map it to DashboardDto.