Tuesday, April 21, 2009

The ContextType property of control 'MyEntityDataSource' must specify a data context.

Do you get something like the following?

The ContextType property of control 'MyEntityDataSource' must specify a data context.

It is likely because the ContextType property (of the EntityDataSource) must be specified in code when using ADO.NET Entity Framework (EF) and EntityDataSource.

I got this when I was using an EntityDataSource and trying to use Dynamic Data (GridView in this case). I forgot that with ADO.NET Entity Framework, the ContextType needs to be set in code for some reason. The best (and maybe only) place is in the Page_Init. Here is the code that will hopefully fix the issue for you also.

protected void Page_Init(object send, EventArgs e)
{
 dsReviewer.ContextType = typeof(DataModel.MyEntities);
 DynamicDataManager1.RegisterControl(gvReviewers);
}

Alternatively, you can set the ContextTypeName in code or using the Properties Window in the Visual Studio 2008. The important thing to remember is to include the namespace. So, in this case, you would set the ContextTypeName to DataModel.MyEntities

Note: The RegisterControl line is not required to fix this issue, but does fix another issue when using DynamicData with the ADO.NET Entity Framework (EF).

2 comments:

Anonymous said...

Thanks for the post. This fixed my problem.

Brent V said...

Awesome! Glad to help!

Brent