ahmad molaie
ahmad molaie

Reputation: 1540

migrating old ado.net web app to EF

consider this query

Select (some properties from all 3 Tables)
    From PageWidgets pw LEFT JOIN PageWidgetDefinition pwd 
       On pwd.ID = pw.WidgetID
    LEFT JOIN PageWidgetSkin pws 
       ON pws.ID  = pw.SkinInstanceID 
    LEFT JOIN PageWidgetSkinRows pwsr 
       On pwsr.SkinID = pws.ID Where pw.PageID = *x*
    Order By (some properties)

in old implementation, it reads widgets on a page & their skin & i have a function looping through rows returned & make a pagewidget by its skin & its widget instance. each widget has three row for its skin, and finally we receive a List that has everything it needs to operate

i have these classes in EF

public partial class Widget: BaseEntity {
    public int ID { get; set; }

    public int PageTemplateID { get; set; }
    public PageTemplate PageTemplate { get; set; }

    public int WidgetDefinitionID { get; set; }
    public WidgetDefinition WidgetDefinition { get; set; }

    public int WidgetSkinID { get; set; }
    public WidgetSkin WidgetSkin { get; set; }
            //other properties omitted

}


public partial class WidgetDefinition: BaseEntity {             
    public int ID { get; set; }
    public string Title { get; set; }
            //other properties omitted
    public virtual ICollection<Widget> Widgets { get; set; }
}



public partial class WidgetSkin: BaseEntity {
    public int ID { get; set; }
    public string Name { get; set; }
            //other properties omitted

    public virtual ICollection<Widget> Widgets { get; set; }
    public virtual ICollection<WidgetSkinRow> WidgetSkinRows { get; set; }
}



public partial class WidgetSkinRow: BaseEntity {

    public int ID { get; set; }

    public int WidgetSkinID { get; set; }
    public virtual WidgetSkin WidgetSkin { get; set; }
}

do i need an extra bussiness layer doing the same thing?

using EF, I want to have only one trip to DB.

Upvotes: 0

Views: 38

Answers (1)

GTG
GTG

Reputation: 4954

You can use "eager loading" method to do this.

Your query will then look something like this:

using (var entities = new WidgetEntities() )
{
  var query = from w in entities.Widgets.Include("WidgetDefinition").Include("WidgetDefinition.Widgets").Include("WidgetSkins").Include("WidgetSkins.WidgetSkinRows")
    where w.Page = *x*
    order by  w.someproperty
    select w;

    Widget myWidget = query.First();
} 

Upvotes: 1

Related Questions