﻿
{"id":3752,"date":"2016-03-24T05:40:00","date_gmt":"2016-03-24T05:40:00","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=3752"},"modified":"2016-03-24T05:42:22","modified_gmt":"2016-03-24T05:42:22","slug":"entity-framework-code-first-onmodelcreating-metodu","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/entity-framework-code-first-onmodelcreating-metodu\/","title":{"rendered":"Entity Framework &#8211; Code First OnModelCreating Metodu"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Entity Framework &#8211; Code First yap\u0131s\u0131na \u00f6nceki yaz\u0131lar\u0131m\u0131zda giri\u015f yapm\u0131\u015ft\u0131k. \u00d6nceki yaz\u0131lara g\u00f6z atmak i\u00e7in <a href=\"http:\/\/www.gencayyildiz.com\/blog\/category\/entity-framework\/\" target=\"_blank\">Entity Framework<\/a> isimli kategoriye gitmeniz yeterlidir. Bu yaz\u0131m\u0131zda ise Code First yakla\u015f\u0131m\u0131nda DbContext \u00fczerinden gelen OnModelCreating metodunu inceleyece\u011fiz.<\/p>\n<p><em><strong>OnModelCreating Metodu Nas\u0131l \u00c7al\u0131\u015f\u0131r? Ne \u0130\u015flev G\u00f6rmektedir?<\/strong><\/em><br \/>\nOnModelCreating metodu, veritaban\u0131 ilk defa olu\u015fturulurken tetiklenen bir virtual metotdur. DbContext i\u00e7erisinde bulunur. Code First yap\u0131s\u0131nda DbContext&#8217;ten miras alarak olu\u015fturdu\u011fumuz Context s\u0131n\u0131f\u0131m\u0131zda override ederek kullanaca\u011f\u0131z. Bu metod sayesinde veritaban\u0131 tablolar\u0131 olu\u015fturulmadan araya girecek, tablo isimlerine m\u00fcdahale edebilecek veya kolonlara istedi\u011fimiz ayarlar\u0131 ger\u00e7ekle\u015ftirebilece\u011fiz.<\/p>\n<p>\u015eimdi a\u015fa\u011f\u0131daki kod blo\u011funu inceleyiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class OkulDBContext : DbContext\r\n    {\r\n        public DbSet&lt;Ogrenci&gt; Ogrenciler { get; set; }\r\n        public DbSet&lt;Ogretmen&gt; Ogretmenler { get; set; }\r\n        public DbSet&lt;Memur&gt; Memurlar { get; set; }\r\n    }\r\n<\/pre>\n<p>Bu \u015fekilde bir Context&#8217;i \u00e7al\u0131\u015ft\u0131r\u0131p, veritaban\u0131n\u0131 olu\u015fturdu\u011fumuz zaman tablolarda a\u015fa\u011f\u0131daki gibi bir isimlendirmeyle kar\u015f\u0131la\u015f\u0131yoruz.<\/p>\n<div id=\"attachment_3761\" style=\"width: 185px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/entity-framework-code-first-onmodelcreating-metodu\/entity-framework-code-first-onmodelcreating-metodu\/\" rel=\"attachment wp-att-3761\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3761\" class=\"size-full wp-image-3761\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/03\/Entity-Framework-Code-First-OnModelCreating-Metodu.png\" alt=\"Entity Framework - Code First OnModelCreating Metodu\" width=\"175\" height=\"168\" \/><\/a><p id=\"caption-attachment-3761\" class=\"wp-caption-text\">Entity Framework &#8211; Code First OnModelCreating Metodu<\/p><\/div>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi olu\u015fturmak istedi\u011fim tablolar\u0131n isimlerinin sonuna &#8216;s&#8217; tak\u0131s\u0131 koyulmakta, bu \u015fekilde anlams\u0131z bir veritaban\u0131 dizayn\u0131yla kar\u015f\u0131la\u015f\u0131lmaktad\u0131r.<\/p>\n<h4><span style=\"color: #008000;\">Tablo Ad\u0131n\u0131 Ayarlama<\/span><\/h4>\n<p>OnModelCreating metodu ile tablolar olu\u015fturulmadan araya girerek a\u015fa\u011f\u0131daki gibi tablo isimlerini belirleyebiliyoruz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class OkulDBContext : DbContext\r\n    {\r\n        public DbSet&lt;Ogrenci&gt; Ogrenciler { get; set; }\r\n        public DbSet&lt;Ogretmen&gt; Ogretmenler { get; set; }\r\n        public DbSet&lt;Memur&gt; Memurlar { get; set; }\r\n\r\n        protected override void OnModelCreating(DbModelBuilder modelBuilder)\r\n        {\r\n            modelBuilder.Entity&lt;Ogrenci&gt;().ToTable(&quot;Ogrenciler&quot;);\r\n            modelBuilder.Entity&lt;Ogretmen&gt;().ToTable(&quot;Ogretmenler&quot;);\r\n            modelBuilder.Entity&lt;Memur&gt;().ToTable(&quot;Memurlar&quot;);\r\n        }\r\n    }\r\n<\/pre>\n<p>Bu \u015fekilde al\u0131nan sonu\u00e7 a\u015fa\u011f\u0131daki gibidir.<\/p>\n<div id=\"attachment_3762\" style=\"width: 174px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/entity-framework-code-first-onmodelcreating-metodu\/entity-framework-code-first-onmodelcreating-metodu-2\/\" rel=\"attachment wp-att-3762\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3762\" class=\"size-full wp-image-3762\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/03\/Entity-Framework-Code-First-OnModelCreating-Metodu-1.png\" alt=\"Entity Framework - Code First OnModelCreating Metodu\" width=\"164\" height=\"161\" \/><\/a><p id=\"caption-attachment-3762\" class=\"wp-caption-text\">Entity Framework &#8211; Code First OnModelCreating Metodu<\/p><\/div>\n<p>ToTable metodunun a\u015fa\u011f\u0131daki gibi ikinci parametresinide kullan\u0131rsak e\u011fer olu\u015fturulacak tablonun varsay\u0131lan(dbo) \u015femas\u0131 yerine verilen de\u011feri \u015fema olarak belirtebilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class OkulDBContext : DbContext\r\n    {\r\n        public DbSet&lt;Ogrenci&gt; Ogrenciler { get; set; }\r\n        public DbSet&lt;Ogretmen&gt; Ogretmenler { get; set; }\r\n        public DbSet&lt;Memur&gt; Memurlar { get; set; }\r\n\r\n        protected override void OnModelCreating(DbModelBuilder modelBuilder)\r\n        {\r\n            modelBuilder.Entity&lt;Ogrenci&gt;().ToTable(&quot;Ogrenciler&quot;, &quot;okul&quot;);\r\n            modelBuilder.Entity&lt;Ogretmen&gt;().ToTable(&quot;Ogretmenler&quot;, &quot;okul&quot;);\r\n            modelBuilder.Entity&lt;Memur&gt;().ToTable(&quot;Memurlar&quot;, &quot;okul&quot;);\r\n        }\r\n    }\r\n<\/pre>\n<div id=\"attachment_3763\" style=\"width: 172px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/entity-framework-code-first-onmodelcreating-metodu\/entity-framework-code-first-onmodelcreating-metodu-3\/\" rel=\"attachment wp-att-3763\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3763\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/03\/Entity-Framework-Code-First-OnModelCreating-Metodu-2.png\" alt=\"Entity Framework - Code First OnModelCreating Metodu\" width=\"162\" height=\"164\" class=\"size-full wp-image-3763\" \/><\/a><p id=\"caption-attachment-3763\" class=\"wp-caption-text\">Entity Framework &#8211; Code First OnModelCreating Metodu<\/p><\/div>\n<h4><span style=\"color: #008000;\">Kolon \u00d6zelliklerini\u00a0Ayarlama<\/span><\/h4>\n<p>Tablolar olu\u015fturulmadan nas\u0131l araya girerek tablo ad\u0131n\u0131 ayarlad\u0131ysak, ayn\u0131 mant\u0131kla kolonlar i\u00e7inde ayarlama ger\u00e7ekle\u015ftirebiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        protected override void OnModelCreating(DbModelBuilder modelBuilder)\r\n        {\r\n          ...\r\n            modelBuilder.Entity&lt;Ogrenci&gt;().Property(p =&gt; p.Adi)\r\n                .HasColumnName(&quot;Adi&quot;)\r\n                .HasColumnOrder(2)\r\n                .HasMaxLength(30);\r\n          ...\r\n        }\r\n<\/pre>\n<p>Yukar\u0131daki \u00f6rnek kod blo\u011funu incelerseniz e\u011fer sadece Ogrenci tablosuna \u00f6rneklendirme yap\u0131lm\u0131\u015ft\u0131r. \u00d6rnek ama\u00e7l\u0131 &#8220;Adi&#8221; kolunu se\u00e7ilmi\u015f ve &#8220;HasColumnName&#8221; metodu ile ad\u0131n\u0131, &#8220;HasColumnOrder&#8221; metodu ile s\u0131ralamas\u0131n\u0131 ve &#8220;HasMaxLength&#8221; metodu ile de karakter say\u0131s\u0131n\u0131 belirtmi\u015f oldum. Tabi burada \u00f6rneklendirme ama\u00e7l\u0131 s\u0131n\u0131rl\u0131 metodlar se\u00e7ilmi\u015ftir. Siz kurcalayarak daha geni\u015f bilgi edinebilirsiniz. <\/p>\n<div id=\"attachment_3767\" style=\"width: 304px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.gencayyildiz.com\/blog\/entity-framework-code-first-onmodelcreating-metodu\/entity-framework-code-first-onmodelcreating-metodu-4\/\" rel=\"attachment wp-att-3767\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3767\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/03\/Entity-Framework-Code-First-OnModelCreating-Metodu-3.png\" alt=\"Entity Framework - Code First OnModelCreating Metodu\" width=\"294\" height=\"38\" class=\"size-full wp-image-3767\" \/><\/a><p id=\"caption-attachment-3767\" class=\"wp-caption-text\">Entity Framework &#8211; Code First OnModelCreating Metodu<\/p><\/div>\n<p>Bu yaz\u0131m\u0131z\u0131nda sonuna gelmi\u015f bulunmaktay\u0131z.<br \/>\nZahmet edip, okudu\u011funuz i\u00e7in te\u015fekk\u00fcr ederim&#8230;<\/p>\n<p>Sonraki 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, Entity Framework &#8211; Code First yap\u0131s\u0131na \u00f6nceki yaz\u0131lar\u0131m\u0131zda giri\u015f yapm\u0131\u015ft\u0131k. \u00d6nceki yaz\u0131lara g\u00f6z atmak i\u00e7in Entity Framework isimli kategoriye gitmeniz yeterlidir. Bu yaz\u0131m\u0131zda ise Code First yakla\u015f\u0131m\u0131nda DbContext \u00fczerinden gelen OnModelCreating metodunu inceleyece\u011fiz.&#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":3739,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[955],"tags":[952,975,963,979,980,981,977,974,976,978],"class_list":["post-3752","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-entity-framework","tag-code-first","tag-ef","tag-entity-framework","tag-hascolumnname","tag-hascolumnorder","tag-hasmaxlength","tag-modelbuilder","tag-onmodelcreating","tag-table-name","tag-totable"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/3752","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=3752"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/3752\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3739"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=3752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=3752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=3752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}