将旧的ado.net网络应用程序迁移到EF

时间:2012-09-06 16:47:19

标签: entity-framework

考虑此查询

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)

在旧的实现中,它在页面上读取小部件&他们的皮肤和我有一个函数循环返回行和&通过皮肤制作一个页面小工具它的小部件实例。 每个小部件的外观都有三行,最后我们收到一个列表,其中包含操作所需的一切

我在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; }
}

我是否需要额外的业务层来做同样的事情?

使用EF,我想只有一次DB。

1 个答案:

答案 0 :(得分:1)

您可以使用“预先加载”方法来执行此操作。

您的查询将如下所示:

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();
}