﻿{"id":462,"date":"2019-01-13T12:46:30","date_gmt":"2019-01-13T11:46:30","guid":{"rendered":"https:\/\/blog.pjsen.eu\/?p=462"},"modified":"2019-01-13T12:46:30","modified_gmt":"2019-01-13T11:46:30","slug":"the-worst-entity-framework-pitfall","status":"publish","type":"post","link":"https:\/\/blog.pjsen.eu\/?p=462","title":{"rendered":"The worst Entity Framework pitfall"},"content":{"rendered":"\n<p>\nI work with a quite big enterprise system in my job. Not surprisingly, it uses Entity Framework (Core, but it does not matter) and SQL Server. The system consists of multiple reusable components also in the data access layer. I had to modify <em>DbContext<\/em> and write some flexible and reusable method accepting a predicate as an argument and apply the predicate on a <em>DbContext<\/em>. Let&#8217;s assume we are using the table <em>A<\/em> from the previous post. I happily coded the signature of the method to use <code>Func<a , bool><\/a><\/code>. Let&#8217;s simulate this in the LINQPad and run our <code>Func<a , bool><\/a><\/code> against a <em>DbContext<\/em>.\n<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"471\" height=\"331\" src=\"https:\/\/blog.pjsen.eu\/wp-content\/uploads\/2019\/01\/efpredfunc.png\" alt=\"\" class=\"wp-image-465\" srcset=\"https:\/\/blog.pjsen.eu\/wp-content\/uploads\/2019\/01\/efpredfunc.png 471w, https:\/\/blog.pjsen.eu\/wp-content\/uploads\/2019\/01\/efpredfunc-300x211.png 300w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/figure><\/div>\n\n\n\n<p>\nIt did not work. Or&#8230; did it? The picture above shows only generated SQL, but I promise the results show correctly the one record. <strong>The problem is, the predicate has been applied in memory after having pulled all the records from table A into memory as well<\/strong>. I am not going to explain what it means for any reasonably sized system. The correct way of doing this is to use <code>Expression&lt;Func&lt;A, bool&gt;&gt;<\/code>.\n<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"550\" height=\"400\" src=\"https:\/\/blog.pjsen.eu\/wp-content\/uploads\/2019\/01\/efpredexprfunc.png\" alt=\"\" class=\"wp-image-467\" srcset=\"https:\/\/blog.pjsen.eu\/wp-content\/uploads\/2019\/01\/efpredexprfunc.png 550w, https:\/\/blog.pjsen.eu\/wp-content\/uploads\/2019\/01\/efpredexprfunc-300x218.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/figure><\/div>\n\n\n\n<p>\nThe explanation is in fact really obvious for anyone deeply understanding how <em>ORM<\/em>s work. The data structure which allows for inspecting a predicate on the fly and building final SQL query is <code>Expression<\/code>. There is already an infrastructure for so-called expression visitors. Please also note, that you can always get your <code>Func<\/code> from <code>Expression&lt;Func&gt;<\/code> by calling <code>Compile<\/code> method on it. \n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I work with a quite big enterprise system in my job. Not surprisingly, it uses Entity Framework (Core, but it does not matter) and SQL Server. The system consists of multiple reusable components also in the data access layer. I had to modify DbContext and write some flexible and reusable method accepting a predicate as<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-462","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts\/462","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=462"}],"version-history":[{"count":0,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts\/462\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}