﻿{"id":390,"date":"2016-06-23T22:16:06","date_gmt":"2016-06-23T20:16:06","guid":{"rendered":"http:\/\/blog.pjsen.eu\/?p=390"},"modified":"2022-02-22T12:57:05","modified_gmt":"2022-02-22T11:57:05","slug":"entity-object-in-ef-is-partially-silently-read-only","status":"publish","type":"post","link":"https:\/\/blog.pjsen.eu\/?p=390","title":{"rendered":"Entity object in EF is partially silently read-only"},"content":{"rendered":"<p>What this post is all about is the following program written in C# using Entity Framework 6.1.3 throwing at line 25 and not at line 23. <\/p>\n<p>We can see the simplest usage of Entity framework here. There is a <code>Test<\/code> class and a <code>TestChild<\/code> class which contains a reference to an instance of <code>Test<\/code> named <code>Parent<\/code>. This reference is marked as <code>virtual<\/code> so that Entity Framework in instructed to load an instance in a lazy manner, i.e. upon first usage of that reference. In DDL model obviously <code>TestId<\/code> column is a foreign key to <code>Test<\/code> table. <\/p>\n<p>I create an entity object, save it into database and then I retrieve it at line 21. Because the class uses virtual properties, Entity Framework dynamically creates some custom type in order to be able to implement lazy behavior underneath.<\/p>\n<p>Now let&#8217;s suppose I have a need to modify something in an object retrieved from a database. It turns out I can modify <code>Value<\/code> property, which is of pure <code>string<\/code> type. What is more, I can also modify <code>Parent<\/code> property, but&#8230; <strong>the modification is not preserved!<\/strong>. This program throws at line 25 because an assignment from line 24 is silently ignored by the framework.<\/p>\n<p>I actually have been trapped by this when I was in need of modifying some collection in complicated object graph. I am deeply disappointed the Entity Framework on one hand allows modification of non-virtual properties, but on the other hand it ignores virtual ones. This can make a developer run into a trouble. <\/p>\n<p>Of course I am aware it is not good practice to work on objects of data model classes. I recovered myself from this situation with AutoMapper. But this is kind of a quirk, and a skilled developer has to hesitate to even try to modify something returned by Entity Framework.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/przemsen\/97eaf5028e91b9111fae417055eb9c3e.js?file=blog-2016-06-23-cs1.cs\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What this post is all about is the following program written in C# using Entity Framework 6.1.3 throwing at line 25 and not at line 23. We can see the simplest usage of Entity framework here. There is a Test class and a TestChild class which contains a reference to an instance of Test named<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,7],"tags":[],"class_list":["post-390","post","type-post","status-publish","format-standard","hentry","category-net","category-quick-tip"],"_links":{"self":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts\/390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=390"}],"version-history":[{"count":0,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts\/390\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}