Entity DataSource not working with Entity Framework 6 Upgrade

Maninder picture Maninder · Nov 5, 2013 · Viewed 13.4k times · Source

I recently upgraded our Webforms application from EF 4.4 to EF6 and I got so many compile time build errors with the Entity Datasource controls. Generally I am getting these error in all occurrences when trying to access the context object :

e.Context

I have followed the same walkthrough as given in : http://entityframework.codeplex.com/wikipage?title=Updating%20Applications%20to%20use%20EF6

The error information which is showing up is:

1) Module System.Data.Entity. version="4.0.0.0" should be referenced

2) Cannot case expression of type System.Data.Objects.ObjectContext to type ObjectContext (After explicitly type casting to System.Data.Entity.Core.Objects.ObjectContext)

Can anybody help to fix this?

Answer

Sergey picture Sergey · Jul 31, 2014

I just went through this exercise when upgrading to EF 6 from EF 5 and I had the same errors.

Here is what I had to do.

Install-Package Microsoft.AspNet.EntityDataSource

It would register a new EntityDataSource Control in the web.config under pages:

<pages>
  <controls>
    <add tagPrefix="ef" assembly="Microsoft.AspNet.EntityDataSource" namespace="Microsoft.AspNet.EntityDataSource" />
  </controls>
</pages>

Next step is to replace existing <asp:EntityDataSource /> controls to <ef:EntityDataSource /> in your aspx pages.

Final step is to go in your code behind and update references for EntityDataSourceContextCreatingEventArgs or any other type of EFContext tags.

From

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

To

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

It all worked and I didn't have to reference System.Data.Entity in the assembly.