How to use the EntityDataSource control with the DbContext API

How to use the EntityDataSource control with the DbContext API

  • Comments 3

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" 

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 6 and type the answer here:
  • Post
  • I try it, But if I Try to show the related column name, y received text like this


    instead of the name


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


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

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

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



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



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



    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)




  • @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">


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



  • I have tried this approach but it is not working properly. For example, in the EntityDatasource's updating event when I 'm getting the entity back from the event args, I'm losing the "navigation property" values:

    protected void EntityDataSource_Updating(object sender, Microsoft.AspNet.EntityDataSource.EntityDataSourceChangingEventArgs e)


       var school = (School) e.Entity;

       school.Departments.... //NO data is coming for "Departments" navigation property

       var name = school.Name;  //Getting the "Name" property value


    ...but I'm getting the property values of the entity.

Page 1 of 1 (3 items)