【发布时间】:2012-11-08 03:45:11
【问题描述】:
使用 EntityFramework,我在 App_Code\DAL 中创建了一个 EntityDataModel(.edmx 文件)。在向导中,我将实体命名为“DLGDBEntities”。我在 .aspx 中有许多 EntityDataSources,我将 OnContextCreating 属性设置为“UseSurveyContext”,如下所示:
protected void UseSurveyContext(object sender, EntityDataSourceContextCreatingEventArgs e)
{
e.Context = surveyContext;
}
surveyContext的设置代码如下:
DLGDBEntities surveyContext;
在 Page_Load 中:
surveyContext = new DLGDBEntities();
以上所有代码看起来都与我在每个教程中看到的代码相同(例如:http://msdn.microsoft.com/en-us/library/cc668193.aspx#1),我可以发誓我已经让它工作了。
但是,现在我收到错误消息:Cannot implicitly convert type 'DAL.DLGDBEntities' to 'System.Data.Objects.ObjectContext'
我做错了什么,为什么以前会起作用?
【问题讨论】:
-
你的 DLGDBEntites 是从 DbContext 派生的吗?
-
是的,因为向导就是这样做的。它应该来自 ObjectContext 吗?
-
EntityDataSource 控件仅适用于 ObjectContext。您可以使用
((IObjectContextAdapter)context).ObjectContext从 DbContext 中获取 ObjectContext - 希望它会有所帮助。 -
非常感谢帕维尔。这有效,我正在再次编译。但我仍然很困惑。我问题中的 msdn 链接与我所做的完全相同。为什么它不必将实体强制转换为 ObjectContext?
-
您必须使用 VS2012。在 VS2012 中,默认代码生成器已更改为生成 POCO 实体和 DBContext,而不是从 VS2010 中默认的 EntityObject 和 ObjectContext 派生的实体。 msdn文章是为VS2010写的
标签: c# entity-framework objectcontext