29

I have a webforms project using EF codefirst to persist data. I'd like to use a GridView and EntityDataSource, in order to save writing CRUD. Is this possible?

Can I convert my DBContext to an ObjectContext that is expected by the EntityDataSource?

Here's what I tried:

<asp:EntityDataSource ID="OrdersDataSource" runat="server" ContextTypeName="SomeNamespace.Models.ShopDBContext" 
     EnableFlattening="False" EntitySetName="Orders" EntityTypeFilter="Order" EnableDelete="False" 
     EnableUpdate="False" Include="OrderLines" OrderBy="it.Id"> 
</asp:EntityDataSource>

<asp:GridView ID="OrdersGridView" runat="server" AllowPaging="True" AllowSorting="True" 
     AutoGenerateColumns="True" DataKeyNames="Id" DataSourceID="OrdersDataSource" /> 

However I get this exception:

Unable to cast object of type 'SomeNamespace.Models.ShopDBContext' to type 'System.Data.Objects.ObjectContext'.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Myster
  • 17,704
  • 13
  • 64
  • 93
  • 2
    possible duplicate of [How to bind EF Code First DbContext to an Asp.Net DataSource?](http://stackoverflow.com/questions/6327937/how-to-bind-ef-code-first-dbcontext-to-an-asp-net-datasource) – Ladislav Mrnka Nov 09 '11 at 09:19
  • Indeed it is a duplicate, thanks (note: a search for "DBContext ObjectContext GridView" will find this but not the question with an answer) – Myster Nov 09 '11 at 21:31

3 Answers3

74

Try this:

var context = new YourDbContext();
var adapter = (IObjectContextAdapter)context;
var objectContext = adapter.ObjectContext;
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
marianosz
  • 1,234
  • 10
  • 8
  • 1
    god been looking all over the place for those lines. Thanks! – forhas Feb 13 '13 at 00:43
  • 2
    This might be a dumb question but if 'YourDbContext' inherits 'IObjectContextAdapter', why do you have to cast it? Shouldn't 'YourDbContext' instantiate ObjectContext? – Eitan May 11 '13 at 03:37
  • 1
    Because the IObjectContextAdapter interface has only one property (ObjectContext) which is implemented on a private way on the DbContext Class. Check this link http://stackoverflow.com/questions/792908/what-is-a-private-interface – marianosz Jun 24 '13 at 20:57
4

Try this one ->

protected void OrdersDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)  
{   
    var context = new YourContext();
    e.Context = ((IObjectContextAdapter)context).ObjectContext;
}
René Höhle
  • 26,716
  • 22
  • 73
  • 82
2

After 2 days of struggling , I found this link which helped me a lot.I am working withVS 2012 and I had same problem with DBContext.
According to the link, in VS2012 the default code generator was changed to generate POCO entities and DBContext as opposed to entities derived from EntityObject and ObjectContext which was default in VS2010.
In solution explorer, under your entity model, You need to remove tt templates and, in the designer, righ-click on the designer surface and then in the properties change the code generation strategy from None to Default to get EntityObject based entities and ObjectContext derived context.

Community
  • 1
  • 1
Ali Fattahian
  • 495
  • 1
  • 6
  • 24