{"id":22014,"date":"2012-09-13T14:12:34","date_gmt":"2012-09-13T21:12:34","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/aspnet\/?p=22014"},"modified":"2012-09-13T14:12:34","modified_gmt":"2012-09-13T21:12:34","slug":"how-to-use-the-entitydatasource-control-with-the-dbcontext-api","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/how-to-use-the-entitydatasource-control-with-the-dbcontext-api\/","title":{"rendered":"How to use the EntityDataSource control with the DbContext API"},"content":{"rendered":"<p>The EntityDataSource control is designed to work with the ObjectContext API because that&#8217;s all that existed when the control was created. The DbContext API was introduced later, in Entity Framework 4.1 alongside the Code First development workflow.\u00a0 Since Code First and DbContext were introduced together you might be accustomed to associating the two, but now in Entity Framework 5.0 the DbContext API is the recommended and default API for all development workflows \u2013 Database First and Model First as well as Code First.\u00a0 So if you want to use the EntityDataSource control, sooner or later you&#8217;ll want to know how to use it with the DbContext API.<\/p>\n<p>Pranav Rastogi has an excellent post on this blog explaining how to do that in a Dynamic Data project (<a href=\"http:\/\/blogs.msdn.com\/b\/webdev\/archive\/2012\/08\/15\/using-dynamic-data-with-entity-framework-dbcontext.aspx\" target=\"_blank\" rel=\"noopener\">Using Dynamic Data with Entity Framework DbContext<\/a>). For a regular ASP.NET Web Forms application, the workaround is different. What you have to do is handle the context creating event of the data source and provide the underlying ObjectContext instance that you get from IObjectContextAdapter. For example, here is your .aspx markup for the EntityDataSource control:\u00a0<\/p>\n<blockquote>\n<\/blockquote>\n<pre class=\"code\"><span style=\"background: white;color: blue\">&lt;<\/span><span style=\"background: white;color: maroon\">asp<\/span><span style=\"background: white;color: blue\">:<\/span><span style=\"background: white;color: maroon\">EntityDataSource <\/span><span style=\"background: white;color: red\">ID<\/span><span style=\"background: white;color: blue\">=\"SchoolContextEntityDataSource\" <\/span><span style=\"background: white;color: red\">runat<\/span><span style=\"background: white;color: blue\">=\"server\" \n    <\/span><span style=\"background: white;color: red\">OnContextCreating<\/span><span style=\"background: white;color: blue\">=\"SchoolContextEntityDataSource_ContextCreating\"\n    <\/span><span style=\"background: white;color: red\">EntitySetName<\/span><span style=\"background: white;color: blue\">=\"Departments\"&gt;\n&lt;\/<\/span><span style=\"background: white;color: maroon\">asp<\/span><span style=\"background: white;color: blue\">:<\/span><span style=\"background: white;color: maroon\">EntityDataSource<\/span><span style=\"background: white;color: blue\">&gt;\n<\/span><\/pre>\n<p>And here is the code-behind that provides the ObjectContext:<\/p>\n<blockquote>\n<\/blockquote>\n<pre class=\"code\"><span style=\"background: white;color: blue\">protected void <\/span><span style=\"background: white;color: black\">SchoolContextEntityDataSource_ContextCreating(<\/span><span style=\"background: white;color: blue\">object <\/span><span style=\"background: white;color: black\">sender, <\/span><span style=\"background: white\">EntityDataSourceContextCreatingEventArgs <\/span><span style=\"background: white;color: black\">e)\n{\n    <\/span><span style=\"background: white;color: blue\">var <\/span><span style=\"background: white;color: black\">db = <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white\">SchoolContext<\/span><span style=\"background: white;color: black\">();\n    e.Context = (db <\/span><span style=\"background: white;color: blue\">as <\/span><span style=\"background: white\">IObjectContextAdapter<\/span><span style=\"background: white;color: black\">).ObjectContext;\n}<\/span><\/pre>\n<p>IObjectContextAdapter is in the System.Data.Entity.Infrastructure namespace:<\/p>\n<blockquote>\n<\/blockquote>\n<pre class=\"code\"><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">System.Data.Entity.Infrastructure;<\/span><\/pre>\n<p>Thanks to <a href=\"http:\/\/blogs.msdn.com\/b\/diego\/\" target=\"_blank\" rel=\"noopener\">Diego Vega<\/a> for showing me how to do this.<\/p>\n<p>-Tom Dykstra<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The EntityDataSource control is designed to work with the ObjectContext API because that&#8217;s all that existed when the control was created. The DbContext API was introduced later, in Entity Framework 4.1 alongside the Code First development workflow.\u00a0 Since Code First and DbContext were introduced together you might be accustomed to associating the two, but now [&hellip;]<\/p>\n","protected":false},"author":424,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[],"class_list":["post-22014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet"],"acf":[],"blog_post_summary":"<p>The EntityDataSource control is designed to work with the ObjectContext API because that&#8217;s all that existed when the control was created. The DbContext API was introduced later, in Entity Framework 4.1 alongside the Code First development workflow.\u00a0 Since Code First and DbContext were introduced together you might be accustomed to associating the two, but now [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/22014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/424"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=22014"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/22014\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=22014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=22014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=22014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}