Entity DataSource control in a DynamicData app referencing an MVC CodeFirst project

1.6k Views Asked by At

I added a Dynamic Data web app as a second project to an MVC 3 app. The Dynamic Data app references the derived EF 4.1 Context in the MVC 3 app (code first).

DefaultModel.RegisterContext(
    new EFCodeFirstDataModelProvider(() => new MvcApplication1.Models.Context()),
    new ContextConfiguration() { ScaffoldAllTables = true });

I want to try out a Dynamic GridView control but I don't understand how to configure the DataSource control. The connection string from my MVC app didn't work for the control. Nothing shows up in the control's configuration wizard even after a build. I am totally webforms/datasource illiterate. Am I way off here? How do I do this?

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.DynamicData.CustomPages.WebUserControl1" %>

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

<asp:DynamicDataManager ID="DynamicDataManager1" runat="server" />
    <DataControlReference ControlID="GridView1"/>
</asp:DynamicDataManager>

<asp:EntityDataSource ID="EntityDataSource1" runat="server">
</asp:EntityDataSource>
2

There are 2 best solutions below

2
On BEST ANSWER

DD templates are way behind.

Add this to a file:

using System.Linq;
using System.Web.DynamicData;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
public static class ModeContainer<OC> where OC : ObjectContext
{
    private static MetaModel model = new MetaModel();
    static ModeContainer() { model.RegisterContext(typeof(OC), new ContextConfiguration() { ScaffoldAllTables = true }); }
    public static MetaModel Model { get { return model; } }
    public static MetaTable Table<EO>() where EO : EntityObject { return Model.Tables.First(x => x.EntityType == typeof(EO)); }
}

Then Global can contain this:

routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
{
    Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
    Model = ModeContainer<ContactsModel.ContactsEntities>.Model
});

And then you can do things like this in a page to use dynamic outside of the route:

protected void Page_Init(object sender, EventArgs e)
{
    MetaTable table = ModeContainer<ContactsModel.ContactsEntities>.Table<Person>();
    GridView1.SetMetaTable(table);
}

Along side of grid and source like this:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <SelectedRowStyle BackColor="Azure" />
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
        <asp:DynamicField DataField="Contacts" />
        <asp:DynamicField DataField="Addresses" />
    </Columns>
</asp:GridView>


<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=ContactsEntities" DefaultContainerName="ContactsEntities" EntitySetName="People" EnableDelete="True" EnableInsert="True" EnableUpdate="True" EntityTypeFilter="Person">
</asp:EntityDataSource>
7
On

The EntityDataSource control needs an ObjectContext (Database First context), not a DbContext (Code First context). This tutorial shows how to use EntityDataSource and is less than a year old: http://www.asp.net/web-forms/tutorials/getting-started-with-ef