How to bind EF Code First DbContext to an Asp.Net DataSource?

混江龙づ霸主 提交于 2019-11-30 07:24:35

You can use EntityDataSource as source for your GridView and implement handler for ContextCreating event:

protected void DataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
    var context = new Context();
    e.Context = ((IObjectContextAdapter)context).ObjectContext;
}

Then you just need to configure the data source in the page. EntitySetName should be hopefully same as your DbSet property name exposed on the context.

Other way is using ObjectDataSource which will make a bridge between GridView and DbSet<Animal> but this can be more complex especially if you want bi-didrectional data binding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!