{"id":737,"date":"2007-12-09T04:36:00","date_gmt":"2007-12-09T04:36:00","guid":{"rendered":"\/blogs\/bobb\/post\/Entity-Framework-Beta3-Deleting-without-fetching.aspx"},"modified":"2013-01-03T23:59:56","modified_gmt":"2013-01-04T07:59:56","slug":"entity-framework-beta3-deleting-without-fetching","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/","title":{"rendered":"Entity Framework Beta3 &#8211; Deleting without fetching"},"content":{"rendered":"<p>\nAs a continuation of the previous discussion, here&#39;s a DELETE of a title row without fetching it from the database:\n<\/p>\n<p>\npubsEntities model = new pubsEntities();<br \/>\ntitles deletetitle = new titles();<br \/>\ndeletetitle.title_id = &quot;BU9994&quot;;<br \/>\ndeletetitle.EntityKey = new EntityKey(&quot;pubsEntities.titles&quot;, &quot;title_id&quot;, &quot;BU9994&quot;);<br \/>\nmodel.Attach(deletetitle);<br \/>\nmodel.DeleteObject(deletetitle);<br \/>\nmodel.SaveChanges();\n<\/p>\n<p>\nIn this case, you need to populate both the property (title_id) that corresponds to the primary key and also populate the EntityKey itself. Attach the object to the store (note that all the original values need not be specified), delete the object from the store, and save the changes. This bit of trickery convinces the ObjectStateManager that the object was in the store all the time, that you&#39;d fetched it from the database. Then you deleted it from the store, so they&#39;ll delete it from the database.\n<\/p>\n<p>\nOf course, if you have associated titleauthor rows, you&#39;ll get an error trying to delete that title. As you would in the relational database. Of course, you can put an ON DELETE CASCADE in the database or delete all of the associated rows first. SQL Server 2005 also allows ON DELETE SET NULL and ON DELETE SET DEFAULT, but you&nbsp;relational data model has to allow this;&nbsp;be careful about using these. You can synthesize and delete all the associated rows and the parent in a single round trip if know what the rows keys are in their entirety, but how do you delete a set of rows in a single statement without fetching or knowing all of the primary key pieces (like the associated au_id in titleauthor table)? Better yet&#8230;\n<\/p>\n<p>\nUPDATE without fetch is even going to be even more interesting, because you need to know the original row values. Or how about updating\/deleting a set of rows based on a non-key column, like &quot;UPDATE titles SET royalty = NULL WHERE ytd_sales IS NULL&quot;? <a href=\"http:\/\/blogs.msdn.com\/b\/alexj\/\" class=\"broken_link\">Alex James<\/a>, Metadata PM of the EF team has posted the first in a series of blog entries on doing just these types of things with EF&#8230;it will be interesting to see how far you can take this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a continuation of the previous discussion, here&#39;s a DELETE of a title row without fetching it from the database: pubsEntities model = new pubsEntities(); titles deletetitle = new titles(); deletetitle.title_id = &quot;BU9994&quot;; deletetitle.EntityKey = new EntityKey(&quot;pubsEntities.titles&quot;, &quot;title_id&quot;, &quot;BU9994&quot;); model.Attach(deletetitle); model.DeleteObject(deletetitle); model.SaveChanges(); In this case, you need to populate both the property (title_id) that corresponds [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-737","post","type-post","status-publish","format-standard","hentry","category-data-access"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Entity Framework Beta3 - Deleting without fetching - Bob Beauchemin<\/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:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Entity Framework Beta3 - Deleting without fetching - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"As a continuation of the previous discussion, here&#039;s a DELETE of a title row without fetching it from the database: pubsEntities model = new pubsEntities(); titles deletetitle = new titles(); deletetitle.title_id = &quot;BU9994&quot;; deletetitle.EntityKey = new EntityKey(&quot;pubsEntities.titles&quot;, &quot;title_id&quot;, &quot;BU9994&quot;); model.Attach(deletetitle); model.DeleteObject(deletetitle); model.SaveChanges(); In this case, you need to populate both the property (title_id) that corresponds [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2007-12-09T04:36:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-04T07:59:56+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/\",\"name\":\"Entity Framework Beta3 - Deleting without fetching - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2007-12-09T04:36:00+00:00\",\"dateModified\":\"2013-01-04T07:59:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Access\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Entity Framework Beta3 &#8211; Deleting without fetching\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Entity Framework Beta3 - Deleting without fetching - Bob Beauchemin","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:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/","og_locale":"en_US","og_type":"article","og_title":"Entity Framework Beta3 - Deleting without fetching - Bob Beauchemin","og_description":"As a continuation of the previous discussion, here&#39;s a DELETE of a title row without fetching it from the database: pubsEntities model = new pubsEntities(); titles deletetitle = new titles(); deletetitle.title_id = &quot;BU9994&quot;; deletetitle.EntityKey = new EntityKey(&quot;pubsEntities.titles&quot;, &quot;title_id&quot;, &quot;BU9994&quot;); model.Attach(deletetitle); model.DeleteObject(deletetitle); model.SaveChanges(); In this case, you need to populate both the property (title_id) that corresponds [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/","og_site_name":"Bob Beauchemin","article_published_time":"2007-12-09T04:36:00+00:00","article_modified_time":"2013-01-04T07:59:56+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/","name":"Entity Framework Beta3 - Deleting without fetching - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2007-12-09T04:36:00+00:00","dateModified":"2013-01-04T07:59:56+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/entity-framework-beta3-deleting-without-fetching\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Data Access","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/"},{"@type":"ListItem","position":3,"name":"Entity Framework Beta3 &#8211; Deleting without fetching"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=737"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/737\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}