EntityDataSource and Entity Framework 6

I learned from this link Entity DataSource not working with Entity Framework 6 Upgrade in Sergey's answer, that you can drop the old EntityDataSource from the toolbox to the designer and then change the tag prefix to ef instead of asp. (I got to the link in Seminda's comment to the question here). After that you can continue working with it in the designer and properties window and it works.

The answer there mentions that if you use events of the EntityDataSource you have to change the code behind

From

protected void OnContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e){... } 

TO

protected void OnContextCreating(object sender, Microsoft.AspNet.EntityDataSource.EntityDataSourceContextCreatingEventArgs e){... } 

The point being to add Microsoft.AspNet.EntityDataSource to the EventArgs.

Also remove the properties DefaultContainerName and ConnectionString, and just set ContextTypeName on the EntityDataSource, as mentioned in LMK's comment there.

I hope this can help those who still want to use the EntityDataSource with the designer and not only thru markup.


I ran into this problem as well and I found "Model Binding" http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data

The post runs through the process of building "Code First" data model classes, however I just switched the ItemType with the classes generated through the "EF Designer from database..." process and it worked just fine.

Hope this helps someone who is still searching.

Mike


Dov Miller's answer was close but didn't work for me because I didn't understand what name I put for ContextTypeName. I created my model with the designer and I didn't find any name derived from ObjectContext in the solution.

So I did this after looking at many threads:

  1. Instead of ContextTypeName, added EntitySetName property pointing to the Set property of my class.

  2. Selected EntityDataSource and double-clicked on the event OnContextCreating to create an event method. Entered the following code in it to convert the DbContext into ObjectContext and resolved the IObjectContextAdapter to add proper using clause.

    var context = new MyModelContainer(); e.Context = ((IObjectContextAdapter)context).ObjectContext;

It finally worked as a datasource for the gridview.

Found the above solution in this thread where the answer by user2076170 shows the event code in step 2 above. I found step 1 on my own.


The user interface isn't supported when using EF6. We no longer recommend using the Entity Data Source for new projects, so we just did the work to provide a data source that is usable with EF6. You need to perform configuration directly in the markup.