How to use the EntityDataSource control with the DbContext API

How to use the EntityDataSource control with the DbContext API

  • Comments 2

The EntityDataSource control is designed to work with the ObjectContext API because that's all that existed when the control was created. The DbContext API was introduced later, in Entity Framework 4.1 alongside the Code First development workflow.  Since Code First and DbContext were introduced together you might be accustomed to associating the two, but now in Entity Framework 5.0 the DbContext API is the recommended and default API for all development workflows – Database First and Model First as well as Code First.  So if you want to use the EntityDataSource control, sooner or later you'll want to know how to use it with the DbContext API.

Pranav Rastogi has an excellent post on this blog explaining how to do that in a Dynamic Data project (Using Dynamic Data with Entity Framework DbContext). For a regular ASP.NET Web Forms application, the workaround is different. What you have to do is handle the context creating event of the data source and provide the underlying ObjectContext instance that you get from IObjectContextAdapter. For example, here is your .aspx markup for the EntityDataSource control: 

<asp:EntityDataSource ID="SchoolContextEntityDataSource" runat="server" 
    OnContextCreating="SchoolContextEntityDataSource_ContextCreating"
    EntitySetName="Departments">
</asp:EntityDataSource>

And here is the code-behind that provides the ObjectContext:

protected void SchoolContextEntityDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
    var db = new SchoolContext();
    e.Context = (db as IObjectContextAdapter).ObjectContext;
}

IObjectContextAdapter is in the System.Data.Entity.Infrastructure namespace:

using System.Data.Entity.Infrastructure;

Thanks to Diego Vega for showing me how to do this.

-Tom Dykstra

Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
  • I try it, But if I Try to show the related column name, y received text like this

    System.Data.Entity.DynamicProxies.Materie_4E5AA4E0247E6E46469E7C8983196D163AF1478706459AD6DAF0070693FEBB44

    instead of the name

    CODE

    <asp:GridView ID="GridViewClassiMaterie"  runat="server" AllowSorting="True" DataSourceID="EntityDataSourceMateriePersonaleScolastico" DataKeyNames="ID_Personale_scolastico_collegato,ID_Materia_collegata,ID_Classe_collegata">

               <Columns>

                   <asp:DynamicField DataField="Materie" HeaderText="Materie" />

                   <asp:DynamicField DataField="Classi" HeaderText="Classi" />

                   <asp:DynamicField DataField="Personale_scolastico" HeaderText="Personale_scolastico" />

               </Columns>

           </asp:GridView>

     <asp:EntityDataSource ID="EntityDataSourceMateriePersonaleScolastico" runat="server"

               EnableDelete="True"  

               EnableFlattening="False"

               EntitySetName="Relazione_Personale_scolastico_Classe_Materia" ConnectionString="name=Segremat8Entities" DefaultContainerName="Segremat8Entities" OnContextCreating="EntityDataSourceMateriePersonaleScolastico_ContextCreating" >

           </asp:EntityDataSource>

    CODE CS

    protected void EntityDataSourceMateriePersonaleScolastico_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)

       {

           var db = new Segremat8Entities();

           e.Context = (db as IObjectContextAdapter).ObjectContext;

       }

    protected void Page_Init(object sender, EventArgs e)

       {

                GridViewClassiMaterie.EnableDynamicData(typeof(Relazione_Personale_scolastico_Classe_Materia));

       }

  • @Kuria,

    Thanks for reporting that. You can get the related data by using a template field with code similar to the following example:

    <asp:TemplateField HeaderText="Administrator Name">

       <ItemTemplate>

              <%# Eval("Administrator.FullName") %>

       </ItemTemplate>

    </asp:TemplateField>

Page 1 of 1 (2 items)