{"id":44,"date":"2015-01-26T22:16:54","date_gmt":"2015-01-26T22:16:54","guid":{"rendered":"http:\/\/webdev.cedarville.edu\/blog\/?p=44"},"modified":"2015-01-26T22:17:59","modified_gmt":"2015-01-26T22:17:59","slug":"objectcontext-disposed-in-entity-framework","status":"publish","type":"post","link":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/","title":{"rendered":"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading"},"content":{"rendered":"<p>Today I ran into an issue with related databases in a C# web application that I am working on. The application accesses SQL Server database tables using the .NET Entity Framework version 6. The error read:<\/p>\n<pre class=\"crayon:false\">The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.<\/pre>\n<p>I had a service object storing a list of results from one table, but the error was thrown when my code in another part of the application tried to access the related items (as designated by the entity framework). When diagnosing the error, I recalled reading about lazy loading in the entity framework set up. Overall lazy loading helps lower the amount of system resources needed since related items are not loaded until needed. Unfortunately, in this instance, it was preventing the web app from accessing those resources due to where and when the original list was being created.<\/p>\n<p>To resolve the error, Stack Overflow came through with a <a href=\"http:\/\/stackoverflow.com\/questions\/24022957\/entity-framework-how-to-disable-lazy-loading-for-specific-query\" target=\"_blank\">solution<\/a> to a similar need that would work for this issue. Here is the C# code I was using that was throwing the error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"Broken Code\">var result = from item in db.Table1\r\n             where item.Term == term\r\n             select item;\r\nreturn result.ToList();<\/pre>\n<p>And here is the update code after I added the <strong>Include()<\/strong>. Note that <strong>Table2Navigation<\/strong> is the name given to the navigation property in Table1 relating it to Table2.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"Updated Code\">var result = from item in db.Table1\r\n             where item.Term == term\r\n             select item;\r\nreturn result.Include(i =&gt; i.Table2Navigation).ToList();<\/pre>\n<p>Once that change was made, my C# web application ran successfully, eliminating the pesky error.<\/p>\n","protected":false},"excerpt":{"rendered":"Today I ran into an issue with related databases in a C# web application that I am working on. The application accesses SQL Server database tables using the .NET Entity Framework version 6. The error read: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. I&#8230; <a class=\"view-article\" href=\"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/\">View Article<\/a>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,7,6],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-c","category-technical","category-web-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading - Cedarville Web Services<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading - Cedarville Web Services\" \/>\n<meta property=\"og:description\" content=\"Today I ran into an issue with related databases in a C# web application that I am working on. The application accesses SQL Server database tables using the .NET Entity Framework version 6. The error read: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. I... View Article\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/\" \/>\n<meta property=\"og:site_name\" content=\"Cedarville Web Services\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-26T22:16:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-01-26T22:17:59+00:00\" \/>\n<meta name=\"author\" content=\"Mark Mazelin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mark Mazelin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/\"},\"author\":{\"name\":\"Mark Mazelin\",\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/#\\\/schema\\\/person\\\/ab92f6846154b230a79d84f494c3f612\"},\"headline\":\"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading\",\"datePublished\":\"2015-01-26T22:16:54+00:00\",\"dateModified\":\"2015-01-26T22:17:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/\"},\"wordCount\":225,\"commentCount\":0,\"articleSection\":[\"C#\",\"Technical\",\"Web Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/\",\"url\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/\",\"name\":\"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading - Cedarville Web Services\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/#website\"},\"datePublished\":\"2015-01-26T22:16:54+00:00\",\"dateModified\":\"2015-01-26T22:17:59+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/#\\\/schema\\\/person\\\/ab92f6846154b230a79d84f494c3f612\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/2015\\\/objectcontext-disposed-in-entity-framework\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/\",\"name\":\"Cedarville Web Services\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/#\\\/schema\\\/person\\\/ab92f6846154b230a79d84f494c3f612\",\"name\":\"Mark Mazelin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/231c381197161fbfc60fca16cb5d0ac8917729e344461b6be5ef3244c1e806a8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/231c381197161fbfc60fca16cb5d0ac8917729e344461b6be5ef3244c1e806a8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/231c381197161fbfc60fca16cb5d0ac8917729e344461b6be5ef3244c1e806a8?s=96&d=mm&r=g\",\"caption\":\"Mark Mazelin\"},\"url\":\"https:\\\/\\\/webdev.cedarville.edu\\\/blog\\\/author\\\/markmazelin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading - Cedarville Web Services","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/","og_locale":"en_US","og_type":"article","og_title":"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading - Cedarville Web Services","og_description":"Today I ran into an issue with related databases in a C# web application that I am working on. The application accesses SQL Server database tables using the .NET Entity Framework version 6. The error read: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. I... View Article","og_url":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/","og_site_name":"Cedarville Web Services","article_published_time":"2015-01-26T22:16:54+00:00","article_modified_time":"2015-01-26T22:17:59+00:00","author":"Mark Mazelin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mark Mazelin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/#article","isPartOf":{"@id":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/"},"author":{"name":"Mark Mazelin","@id":"https:\/\/webdev.cedarville.edu\/blog\/#\/schema\/person\/ab92f6846154b230a79d84f494c3f612"},"headline":"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading","datePublished":"2015-01-26T22:16:54+00:00","dateModified":"2015-01-26T22:17:59+00:00","mainEntityOfPage":{"@id":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/"},"wordCount":225,"commentCount":0,"articleSection":["C#","Technical","Web Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/","url":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/","name":"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading - Cedarville Web Services","isPartOf":{"@id":"https:\/\/webdev.cedarville.edu\/blog\/#website"},"datePublished":"2015-01-26T22:16:54+00:00","dateModified":"2015-01-26T22:17:59+00:00","author":{"@id":"https:\/\/webdev.cedarville.edu\/blog\/#\/schema\/person\/ab92f6846154b230a79d84f494c3f612"},"breadcrumb":{"@id":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webdev.cedarville.edu\/blog\/2015\/objectcontext-disposed-in-entity-framework\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webdev.cedarville.edu\/blog\/"},{"@type":"ListItem","position":2,"name":"ObjectContext disposed in C# Entity Framework and the Need to Disable Lazy Loading"}]},{"@type":"WebSite","@id":"https:\/\/webdev.cedarville.edu\/blog\/#website","url":"https:\/\/webdev.cedarville.edu\/blog\/","name":"Cedarville Web Services","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webdev.cedarville.edu\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/webdev.cedarville.edu\/blog\/#\/schema\/person\/ab92f6846154b230a79d84f494c3f612","name":"Mark Mazelin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/231c381197161fbfc60fca16cb5d0ac8917729e344461b6be5ef3244c1e806a8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/231c381197161fbfc60fca16cb5d0ac8917729e344461b6be5ef3244c1e806a8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/231c381197161fbfc60fca16cb5d0ac8917729e344461b6be5ef3244c1e806a8?s=96&d=mm&r=g","caption":"Mark Mazelin"},"url":"https:\/\/webdev.cedarville.edu\/blog\/author\/markmazelin\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":5,"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":856,"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/posts\/44\/revisions\/856"}],"wp:attachment":[{"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdev.cedarville.edu\/blog\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}