{"id":1350,"date":"2016-06-03T14:23:50","date_gmt":"2016-06-03T04:23:50","guid":{"rendered":"http:\/\/ntsblog.homedev.com.au\/?p=1350"},"modified":"2018-05-31T09:18:38","modified_gmt":"2018-05-30T23:18:38","slug":"entityframework-force-update-object","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2016\/06\/03\/entityframework-force-update-object\/","title":{"rendered":"EntityFramework force the update of an object"},"content":{"rendered":"<div id=\"ntsbl-206472249\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><h2>The Problem<\/h2>\n<p>You are trying to save some changes to an object.<br \/>\nYour code looks like the following:<\/p>\n<pre>\r\n    using (var context = new DbEntities())\r\n    {\r\n        Job job = context.Jobs.Find(jobId);\r\n\r\n        if (job != null)\r\n        {\r\n            job.Processed = true;\r\n            context.SaveChanges();\r\n        }\r\n    }\r\n<\/pre>\n<p>But the changes are not being saved<\/p>\n<h2>The Solution<\/h2>\n<p>Entity framework is not aware that the job object has been changed so you need to tell it, by adding line 8.<\/p>\n<pre>\r\n    using (var context = new DbEntities(_config))\r\n    {\r\n        Job job = context.Jobs.Find(jobId);\r\n\r\n        if (job != null)\r\n        {\r\n            job.Processed = false;\r\n            context.Entry(job).State = EntityState.Modified;\r\n            context.SaveChanges();\r\n        }\r\n    }\r\n<\/pre>\n<p>Note if you wish to control a specific propety and flag that only 1 columns has change<\/p>\n<pre>\r\n    context.Entry(job).Property(\"Processed\").IsModified = true;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Problem You are trying to save some changes to an object. Your code looks like the following: using (var context = new DbEntities()) { [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","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":""},"categories":[5],"tags":[],"class_list":["post-1350","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=1350"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1350\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=1350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=1350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=1350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}