﻿
{"id":11515,"date":"2019-08-07T18:53:27","date_gmt":"2019-08-07T18:53:27","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=11515"},"modified":"2019-08-07T18:57:09","modified_gmt":"2019-08-07T18:57:09","slug":"entity-framework-core-lazy-loading","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/entity-framework-core-lazy-loading\/","title":{"rendered":"Entity Framework Core &#8211; Lazy Loading"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Yaz\u0131l\u0131m uygulamalar\u0131nda temelde esas amac\u0131m\u0131z bir veriyi do\u011fru i\u015fleyebilmek ve varsa \u00fcretilen \u00e7\u0131kt\u0131y\u0131 do\u011fru bir \u015fekilde gerekli yere yans\u0131tabilmektir. Basit bir rehberlik uygulamas\u0131ndan tutun uzaya f\u00fcze g\u00f6ndermeye kalksak bile en temeldeki gayemiz budur ve t\u00fcm \u00e7aba ve in\u015falar\u0131m\u0131z bunun \u00fczerine kuruludur. Keza veri dedi\u011fimiz olgu tek ba\u015f\u0131na b\u00fcy\u00fck anlam ifade etsede, birden fazla veri ili\u015fkisel olarak bir araya gelerek olu\u015fturduklar\u0131 b\u00fct\u00fcn sayesinde daha da b\u00fcy\u00fck anlam ifade edebilmektedirler. Dolay\u0131s\u0131yla yaz\u0131l\u0131m uygulamalar\u0131 genellikle tek bir veri \u00fczerinden de\u011fil, o veriyle ili\u015fkili di\u011fer veriler \u00fczerinden de i\u015flem yapma mecburiyetini g\u00fctmektedirler. Haliyle s\u00fcre\u00e7te elimizde olan veriyle ili\u015fkili t\u00fcm verileri elde etmemiz gerekebilir. \u0130\u015fte bunun i\u00e7in elimizdeki veriyle ili\u015fkisel olan t\u00fcm verileri tek tek getirmek yerine Entity Framework bu i\u015fi \u00fcstlenmekte ve Lazy Loading dedi\u011fimiz Tembel Y\u00fckleme arac\u0131l\u0131\u011f\u0131yla ilgili verileri bizlere ula\u015ft\u0131rmaktad\u0131r.<\/p>\n<p>.NET Core&#8217;dan \u00f6nce Standart Framework mimarisinde Entity Framework yap\u0131lanmas\u0131 varsay\u0131lan olarak a\u00e7\u0131k bir \u015fekilde gelmekteydi. Keza bu varsay\u0131m en basit sorgularda bile l\u00fczumsuz ili\u015fkisel t\u00fcm verileri getirmekte ve hem sorgusal hem de yaz\u0131l\u0131msal y\u00fcksek maliyetlere sebep olmaktayd\u0131. Bu duruma istinaden biz developerlar varsay\u0131lan Lazy Loading mekanizmas\u0131n\u0131 kapat\u0131r ve ihtiyac\u0131m\u0131z do\u011frultusunda Include mekanizmas\u0131n\u0131 devreye sokard\u0131k.<\/p>\n<p>.NET Core mimarisi buradaki varsay\u0131msal durumu de\u011fi\u015ftirerek Entity Framework Core yap\u0131lanmas\u0131nda Lazy Loading mekanizmas\u0131n\u0131 kapatarak, ihtiya\u00e7 dahilinde developer\u0131n iradesine b\u0131rakm\u0131\u015ft\u0131r ve b\u00f6ylece Lazy Loading mekanizmas\u0131n\u0131 bilmeyenler i\u00e7in yersiz maliyetin \u00f6n\u00fcne ge\u00e7mi\u015ftir. \u0130\u015fte bu i\u00e7eri\u011fimizde Entity Framework Core \u00e7al\u0131\u015fmalar\u0131nda Lazy Loading&#8217;i nas\u0131l devreye sokaca\u011f\u0131m\u0131z\u0131 ve nelere dikkat etmemiz gerekti\u011fini inceleyece\u011fiz.<\/p>\n<h3>\u0130lk Olarak<\/h3>\n<p>Her\u015feyden \u00f6nce \u00f6rneklendirme yapabilmek i\u00e7in bir adet Asp.NET Core MVC uygulamas\u0131 a\u00e7\u0131n\u0131z ve ard\u0131ndan ister Code First istersenizde Database First y\u00f6ntemiyle uygulamay\u0131 bir veritaban\u0131 ile ili\u015fkilendiriniz. <\/p>\n<p>Ayr\u0131ca&#8230;<\/p>\n<p>Lazy Loading; Entity Framework Core k\u00fct\u00fcphanesine dahili olarak eklenmemi\u015f, harici olarak Proxy \u015feklinde kullan\u0131labilir bir \u00f6zelli\u011fe \u00e7evrilmi\u015ftir. Bundan dolay\u0131 .NET Core &#8211; EF Core uygulamalar\u0131nda Lazy Loading&#8217;i kullanabilmek i\u00e7in <em><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.EntityFrameworkCore.Proxies\/\" rel=\"noopener noreferrer\" target=\"_blank\">Microsoft.EntityFrameworkCore.Proxies<\/a><\/em> k\u00fct\u00fcphanesini projeye entegre etmeniz gerekmektedir.<\/p>\n<h3>Lazy Loading Proxy Kullan\u0131m\u0131<\/h3>\n<p>EF Core uygulamas\u0131nda Lazy Loading Proxy&#8217;i devreye sokabilmek i\u00e7in context s\u0131n\u0131f\u0131(DbContext&#8217;ten t\u00fcreyen) i\u00e7erisinde override edilen &#8220;OnConfiguring&#8221; metodunda a\u015fa\u011f\u0131daki operasyonun ger\u00e7ekle\u015ftirilmesi gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class MyDBContext : DbContext\r\n    {\r\n        public MyDBContext(DbContextOptions&lt;MyDBContext&gt; dbContext) : base(dbContext) { }\r\n        public DbSet&lt;Personel&gt; Personeller { get; set; }\r\n        public DbSet&lt;Satis&gt; Satislar { get; set; }\r\n\r\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n        {\r\n            optionsBuilder.UseLazyLoadingProxies();\r\n        }\r\n    }\r\n<\/pre>\n<p>Bu i\u015flem neticesinde art\u0131k ilgili uygulamada yap\u0131lan t\u00fcm EF sorgulamalar\u0131nda Lazy Loading \u00e7al\u0131\u015facak ve hedeflenen veriler ili\u015fkisel oldu\u011fu veriler ile b\u00fct\u00fcn halinde elde edilecektir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading.jpg\" alt=\"Entity Framework Core - Lazy Loading\" width=\"648\" height=\"314\" class=\"aligncenter size-full wp-image-11531\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading.jpg 648w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-300x145.jpg 300w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/a><\/p>\n<h3>Olas\u0131 InvalidOperationException Hatas\u0131<\/h3>\n<p>Yukar\u0131daki \u00e7al\u0131\u015fmay\u0131 yapt\u0131\u011f\u0131n\u0131z taktirde InvalidOperationException olas\u0131 hatas\u0131n\u0131 al\u0131yorsan\u0131z a\u015fa\u011f\u0131daki durumlar\u0131 g\u00f6z \u00f6n\u00fcne alarak \u00e7al\u0131\u015fman\u0131z\u0131 d\u00fczenlemenizi \u00f6neririm. Tabi \u00f6ncelikle ilgili hatay\u0131 tam olarak incelememiz gerekirse e\u011fer;<\/p>\n<blockquote style=\"color:orange;\"><p>System.InvalidOperationException: &#8216;Navigation property &#8216;Satislar&#8217; on entity type &#8216;Personel&#8217; is not virtual. UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.&#8217;<\/p><\/blockquote>\n<p>Hatan\u0131n g\u00f6rsel hali ise;<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-1.jpg\" alt=\"Entity Framework Core - Lazy Loading\" width=\"390\" height=\"112\" class=\"aligncenter size-full wp-image-11534\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-1.jpg 390w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-1-300x86.jpg 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><\/a><\/p>\n<p><strong>\u00c7\u00f6z\u00fcm<\/strong><br \/>\nBu hatan\u0131n sebebi, Lazy Loading yap\u0131l\u0131rken entityler\/modeller aras\u0131ndaki ili\u015fkiyi sa\u011flayan Navigation Propertylerin sanal olarak tan\u0131mlanmamas\u0131ndan kaynaklanmaktad\u0131r. Yapmam\u0131z gereken tek i\u015flem t\u00fcm modellerdeki ili\u015fkisel ba\u011flar\u0131 olu\u015fturan propertyleri <em>virtual<\/em> keyword\u00fc ile i\u015faretlemenizdir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-2.jpg\" alt=\"Entity Framework Core - Lazy Loading\" width=\"821\" height=\"494\" class=\"aligncenter size-full wp-image-11536\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-2.jpg 821w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-2-300x181.jpg 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2019\/08\/Entity-Framework-Core-Lazy-Loading-2-768x462.jpg 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/a><br \/>\n\u0130\u015fte \u00e7\u00f6z\u00fcm bu kadar basit \ud83d\ude42<\/p>\n<p>Ve b\u00f6ylece .NET Core uygulamalar\u0131nda EF Core ile Lazy Loading&#8217;in nas\u0131l yap\u0131laca\u011f\u0131n\u0131 detayl\u0131ca incelemi\u015f ve olas\u0131 hatalara dair nas\u0131l \u00e7\u00f6z\u00fcm al\u0131nabilece\u011fini g\u00f6rm\u00fc\u015f olduk. Tabi siz yine ehemmiyeti g\u00f6z ard\u0131 etmeksizin l\u00fczum olmad\u0131\u011f\u0131 taktirde Lazy Loading mekanizmas\u0131n\u0131 m\u00fcmk\u00fcn mertebe pasif tutmaya \u00f6zen g\u00f6stermeyi unutmay\u0131n\u0131z \ud83d\ude42 <\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Yaz\u0131l\u0131m uygulamalar\u0131nda temelde esas amac\u0131m\u0131z bir veriyi do\u011fru i\u015fleyebilmek ve varsa \u00fcretilen \u00e7\u0131kt\u0131y\u0131 do\u011fru bir \u015fekilde gerekli yere yans\u0131tabilmektir. Basit bir rehberlik uygulamas\u0131ndan tutun uzaya f\u00fcze g\u00f6ndermeye kalksak bile en temeldeki gayemiz budur&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":11538,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2739,2914],"tags":[2850,1046,2950],"class_list":["post-11515","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-core","category-entity-framework-core","tag-entity-framework-core","tag-lazy-loading","tag-microsoft-entityframeworkcore-proxies"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/11515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=11515"}],"version-history":[{"count":23,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/11515\/revisions"}],"predecessor-version":[{"id":11542,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/11515\/revisions\/11542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/11538"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=11515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=11515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=11515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}