Question

Using EntityFramework, I have created an EntityDataModel (.edmx file) in App_Code\DAL. In the wizard, I named the entities 'DLGDBEntities'. I have a number of EntityDataSources in the .aspx where I've set the OnContextCreating attribute to 'UseSurveyContext' which looks like this:

protected void UseSurveyContext(object sender, EntityDataSourceContextCreatingEventArgs e)
{
   e.Context = surveyContext;
}

Setup code for the surveyContext is as follows:

DLGDBEntities surveyContext;

and in Page_Load:

surveyContext = new DLGDBEntities();

All of the above looks like the same code I've seen in every tutorial (eg: http://msdn.microsoft.com/en-us/library/cc668193.aspx#1), and I could swear I've had it working.

Now however, I am getting the error: Cannot implicitly convert type 'DAL.DLGDBEntities' to 'System.Data.Objects.ObjectContext'

What have I done wrong, and why did it work before?

Was it helpful?

Solution

EntityDataSource control works only with ObjectContext. You can get your ObjectContext from your DbContext by using ((IObjectContextAdapter)context).ObjectContext. Note that in Visual Studio 2012 the default code generator was changed to generate POCO entities and DBContext as opposed to entities derived from EntityObject and ObjectContext which was the default in Visual Studio 2010.

EDIT

As pointed by Sven in the comments a new, EF6 compatible version of EntityDataSourceControl has been released and it takes the EF6 DbContext instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top