ContextCreating method in EntityDataSource cannot convert Entities to ObjectContext
Asked Answered
B

1

1

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?

Bova answered 8/11, 2012 at 3:45 Comment(5)
Is your DLGDBEntites derived from DbContext?Tonsure
yes, coz that's the way the wizard did it. Should it be deriving from ObjectContext?Bova
EntityDataSource control works only with ObjectContext. You can get your ObjectContext from your DbContext by using ((IObjectContextAdapter)context).ObjectContext - hopefully it will help a bit.Tonsure
Many thanks Pawel. This works and I'm compiling again. But I'm still confusticated. The msdn link in my question is doing exactly the same thing I did. Howcome it doesn't have to cast the Entities to an ObjectContext?Bova
You must be using VS2012. 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. The msdn article was written for VS2010Tonsure
T
9

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.

Tonsure answered 8/11, 2012 at 6:9 Comment(4)
Ahh, yes, I did have a play with VS12. Switched back to VS2010 and didn't even think about that as a possible culprit. So the regeneration overwrote existing code. hmmm. Does the ContextCreating method still return an ObjectContext using VS2012? Many thanks for the assistance.Bova
I must have missed your comment. 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.Tonsure
Currently there is a new Microsoft ASP.NET EntityDataSource control available for EF >= 6.0.0 via NuGet. It accepts the newly DbContext.Tape
I'm using VS13 and Entities6 and this isn't working. I tried opening properties as suggested but code generation strategy is T4 and read only.Stockinet

© 2022 - 2024 — McMap. All rights reserved.