<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Farah Anany on Medium]]></title>
        <description><![CDATA[Stories by Farah Anany on Medium]]></description>
        <link>https://medium.com/@farahanany17?source=rss-f7630ce64b7c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1_UU23dHdJud5C7FbuyN-g@2x.jpeg</url>
            <title>Stories by Farah Anany on Medium</title>
            <link>https://medium.com/@farahanany17?source=rss-f7630ce64b7c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 21:27:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@farahanany17/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Day 7: Data Seeding in Entity Framework]]></title>
            <link>https://medium.com/@farahanany17/day-7-data-seeding-in-entity-framework-49e63fa8b38f?source=rss-f7630ce64b7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/49e63fa8b38f</guid>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[dataseeding]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Farah Anany]]></dc:creator>
            <pubDate>Sat, 20 Dec 2025 09:22:41 GMT</pubDate>
            <atom:updated>2025-12-20T09:22:41.682Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 7: Data Seeding</h3><p>Trying things out requires us to fill in our databases with dummy data so we make sure that everything is working fine. Today’s article is all about that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*UDNeQU8WR4fr8pg4IMGkSg.gif" /></figure><p>Create two classes and their tables:</p><pre> public DbSet&lt;Blog&gt; Blogs { get; set; }<br> public DbSet&lt;Post&gt; Posts { get; set; }<br> public class Blog<br> {<br>     public int BlogId { get; set; }<br>     public string Url { get; set; }<br>     public List&lt;Post&gt; Posts { get; set; }<br><br> }<br> public class Post<br> {<br>     public int PostId { get; set; }<br>     public string Title { get; set; }<br>     public string Content { get; set; }<br>     public int BlogId { get; set; }<br>     public Blog Blog { get; set; }<br> }</pre><p>Using <strong>Fluent API</strong>, give the required dummy data. Do not forget the primary key:</p><pre>  protected override void OnModelCreating(ModelBuilder modelBuilder)<br>  {<br><br>      modelBuilder.Entity&lt;Blog&gt;()<br>                  .HasData(new Blog { BlogId = 1, Url = &quot;www.youtube.com&quot; });<br><br>  }</pre><p>Now, one blog can have many posts.</p><pre>  modelBuilder.Entity&lt;Post&gt;()<br>              .HasData(new Post { PostId=1, BlogId=1,Title=&quot;First Post&quot;, <br>              Content=&quot;Lorem Ipsum&quot;},<br>              new Post { PostId = 2, BlogId = 1, Title = &quot;Second Post&quot;,<br>              Content = &quot;Lorem Ipsum&quot; }<br>              );<br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*pnVzc-AFRAdbbFtFFUb0nQ.gif" /></figure><p>There’s also another way, which is to create function especially for seeding data. Place the function in the Program class. Don’t forget to call it in the <strong>Main</strong> function.</p><pre> public static void SeedData()<br> {<br>     using (var context = new ApplicationDbContext())<br>     {<br>         context.Database.EnsureCreated();<br>         var blog = context.Blogs.FirstOrDefault(b =&gt; b.Url == &quot;www.youtube.com&quot;);<br>         if (blog == null) <br>             context.Blogs.Add(new ApplicationDbContext.Blog { Url = &quot;www.youtube.com&quot; });<br><br>         context.SaveChanges();<br>     }<br> }</pre><p>This uses the context instance, checks if the database created or not, creates it if does not exist. It checks for the existence of a certain value of the Url; if found, it returns it and saves it in blog. If it’s null, it adds a new Url value.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49e63fa8b38f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 6: Indexing in Entity Framework]]></title>
            <link>https://medium.com/@farahanany17/day-6-indexing-in-entity-framework-9c46bc7cda0a?source=rss-f7630ce64b7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c46bc7cda0a</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[entity-framework]]></category>
            <category><![CDATA[indexing]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Farah Anany]]></dc:creator>
            <pubDate>Fri, 19 Dec 2025 09:26:36 GMT</pubDate>
            <atom:updated>2026-01-03T17:18:52.122Z</atom:updated>
            <content:encoded><![CDATA[<p>You’ll find that any foreign keys that get created get automatically indexed. This creation will be found in your migration. But, what if we want to create it on our own?</p><h4>Indexing a Property</h4><p>Firstly, we need to create these two classes:</p><pre> public class Blog<br> {<br>     public int BlogId { get; set; }<br>     public string Url{ get; set; }<br>     public List&lt;Post&gt; Posts { get; set; }<br><br> }<br> public class Post<br> {<br>     public int PostId { get; set; }<br>     public int BlogId{ get; set; }<br>     public Blog Blog { get; set; }<br>   <br> }</pre><p>Don’t forget to create tables of these classes:</p><pre>    public DbSet&lt;Post&gt; Posts { get; set; }<br>    public DbSet&lt;Blog&gt; Blogs { get; set; }</pre><p>Let’s index the <strong>Url</strong> property for example.</p><pre> protected override void OnModelCreating(ModelBuilder modelBuilder)<br> {<br><br>     modelBuilder.Entity&lt;Blog&gt;()<br>                 .HasIndex(b =&gt; b.Url);<br><br> }</pre><p>AND THAT’S IT.</p><h4><strong>Indexing More than One Column/Property</strong></h4><p>Create a <strong>Person</strong> class:</p><pre>  public class Person<br>  {<br>      public int Id { get; set; }<br>      public string FirstName{ get; set; }<br>      public string LastName{ get; set; }<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*M63CdxgOSYSzajY99tlNpQ.gif" /></figure><p>Now, let’s create its table.</p><pre> public DbSet&lt;Person&gt; Persons { get; set; }</pre><p>We want to index both the first and last name using Fluent API.</p><pre> modelBuilder.Entity&lt;Person&gt;()<br>             .HasIndex(p=&gt; new { p.FirstName,p.LastName });</pre><h4>How Could I Make the Index Value Unique ?</h4><p>It’s pretty simple. This is all what you need to do.</p><pre> modelBuilder.Entity&lt;Blog&gt;()<br>             .HasIndex(b =&gt; b.Url)<br>              .IsUnique();</pre><h4>How to Change the Index Name</h4><pre>modelBuilder.Entity&lt;Blog&gt;()<br>             .HasIndex(b =&gt; b.Url)<br>             .HasDatabaseName(&quot;Index_Url&quot;);</pre><h4>Using an Index Filter</h4><p>To come out with outputs based on specific conditions, a filter would be a great solution. Take the <strong>Url</strong> property as an example.</p><pre>         modelBuilder.Entity&lt;Blog&gt;()<br>                     .HasIndex(b =&gt; b.Url)<br>                     .HasFilter(&quot;[Url] IS NOT NULL&quot;);</pre><p>This version allows only one indexed value to equal Null, because we added the <strong>IsUnique</strong>.</p><pre>modelBuilder.Entity&lt;Blog&gt;()<br>            .HasIndex(b =&gt; b.Url)<br>            .IsUnique()<br>            .HasFilter(null);</pre><p>And that’s it for today. Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c46bc7cda0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 5: Table Relationships in Entity Framework]]></title>
            <link>https://medium.com/@farahanany17/day-5-table-relationships-f2f93dd4ac21?source=rss-f7630ce64b7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2f93dd4ac21</guid>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[relational-databases]]></category>
            <dc:creator><![CDATA[Farah Anany]]></dc:creator>
            <pubDate>Thu, 18 Dec 2025 12:31:02 GMT</pubDate>
            <atom:updated>2025-12-18T12:33:20.751Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, we are going to talk about all the possible relationships that tables can have with one another.</p><h4>One-to-One Relationships</h4><p>For this example we’re going to create two classes: <strong>Blog</strong> and <strong>BlogImage</strong>. These two classes are connected via one Foreign Key.</p><pre>  public class Blog<br>  {<br>    public int Id { get; set; }<br>    public string Url { get; set; }<br>    <br>    public BlogImage BlogImage { get; set; }<br>    <br>  <br>  }<br>  public class BlogImage<br>  {<br>      public int Id { get; set; }<br>      public string Image { get; set; }<br>      public string Caption { get; set; }<br>      public int BlogForeignKey {  get; set; }<br>      public Blog Blog { get; set; }<br>  }</pre><p>Add the following lines of code to <strong><em>ApplicationDbContext</em></strong> to create the tables:</p><pre>     public DbSet&lt;Blog&gt; Blogs { get; set; }<br>     public DbSet&lt;BlogImage&gt; BlogImages { get; set; }</pre><p>In our case, the Blog is the principal (parent ) entity and the Blog Image is the child. So, the primary key in the parent (Id) is corresponds to the BlogForeignKey in Blog Image.</p><p>Add the following to the <strong><em>OnModelCreating</em></strong> method:</p><pre> modelBuilder.Entity&lt;Blog&gt;()<br>             .Property(b =&gt; b.Url)<br>             .HasMaxLength(1000)<br>             .IsRequired();<br> modelBuilder.Entity&lt;BlogImage&gt;()<br>           .Property(i =&gt; i.Caption)<br>           .HasMaxLength(250)<br>           .IsRequired();<br> modelBuilder.Entity&lt;Blog&gt;()<br>             .HasOne(b =&gt; b.BlogImage)   // Blog has one BlogImage (Navigation property)<br>             .WithOne(i =&gt; i.Blog)     // BlogImage has one Blog (Inverse navigation property)<br>             .HasForeignKey&lt;BlogImage&gt;(b =&gt; b.BlogForeignKey); // Foreign key defined in BlogImage</pre><h4>One-to-Many Relationships</h4><h4>Example 1:</h4><p>Create these two classes: Post and Blog</p><pre>public class Blog<br>{<br>  public int BlogId { get; set; }<br>  public string Url { get; set; }<br>  <br>  public List&lt;Post&gt; Posts { get; set; }<br><br>}<br>public class Post<br>{<br>    public int PostId { get; set; }<br>    public string Title { get; set; }<br>    public string Content{ get; set; }<br>    public int BlogId{  get; set; }<br>    public Blog Blog { get; set; }<br>}</pre><p>Using the navigation property does not need us to use DbSet again with <strong><em>Post</em></strong> to create the table. It gets created by association. Then, add migration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*KBgBLJbzbO_D_ujtCK5Wvw.gif" /></figure><p>This way each Blog can have many Posts.</p><p>How to configure using Fluent API?</p><p>There are two ways since there are navigation properties in each class.</p><p><strong>Way 1:</strong></p><pre>      modelBuilder.Entity&lt;Blog&gt;()<br>                   .HasMany(b =&gt; b.Posts)<br>                   .WithOne(b =&gt; b.Blog);</pre><p><strong>Way 2:</strong></p><pre>      modelBuilder.Entity&lt;Blog&gt;()<br>                   .HasMany(b =&gt; b.Posts)<br>                   .WithOne(b =&gt; b.Blog);</pre><p>What if we don’t have navigation properties?</p><p>If that’s the case you have to use DbSet to create the Posts Table</p><pre>  public DbSet&lt;Post&gt; Posts { get; set; }</pre><p>Then, use this:</p><pre>modelBuilder.Entity&lt;Post&gt;()<br>    .HasOne(p =&gt; p.Blog)               // Define the navigation property (Post -&gt; Blog)<br>    .WithMany(b =&gt; b.Posts)            // Define the inverse navigation property (Blog -&gt; Posts)<br>    .HasForeignKey(p =&gt; p.BlogId);     // Specify the foreign key in the Post entity</pre><h4>Example 2:</h4><p>Create these two classes: Car and RecordOfSale</p><pre>public class Car<br>{<br>    public int CarId { get; set; }<br>    public string LicensePlate { get; set; }<br>    public string Model {  get; set; }<br><br>    public List&lt;RecordOfSale&gt; SaleHistory { get; set; }<br><br><br>}<br>public class RecordOfSale<br>{<br>    public int RecordOfSaleId { get; set; }<br>    public DateTime DateSold { get; set; }<br>    public decimal Price { get; set; }<br>    public string CarLicensePlate { get; set; }<br><br>    public Car car  { get; set; }<br><br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*bLxHsnxnhx6Q4YmAhOgcdQ.gif" /></figure><p>Don’ forget this line:</p><pre>   public DbSet&lt;Car&gt; Cars { get; set; }</pre><p>To connect the <strong>CarLicensePlate</strong> with the <strong>License Plate</strong>, do the following:</p><pre> modelBuilder.Entity&lt;RecordOfSale&gt;()<br>             .HasOne(s =&gt; s.Car)<br>             .WithMany(c =&gt; c.SaleHistory)<br>             .HasForeignKey(s =&gt; s.CarLicensePlate)<br>             .HasPrincipalKey(c =&gt; c.LicensePlate);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*NIHKWt7CtLGSE1WhY5_IrA.gif" /></figure><h4>Many-to-Many Relationships</h4><p><strong>Example 1:</strong></p><p>Create these two classes:</p><pre>    public class Post<br>    {<br>        public int PostId { get; set; }<br>        public string Title { get; set; }<br>        public string Content { get; set; }<br>        public ICollection&lt;Tag&gt; Tags { get; set; }<br>    }<br>   <br>   public class Tag<br>{<br>    public int TagId { get; set; }<br>    public ICollection&lt;Post&gt; Posts { get; set; }<br>}</pre><p>Don’t forget to use <strong>DbSet</strong> to create their tables.</p><pre> public DbSet&lt;Post&gt; Posts { get; set; }<br> public DbSet&lt;Tag&gt; Tags { get; set; }</pre><p>We want to connect these two tables via a third table:</p><pre> modelBuilder.Entity&lt;Post&gt;()<br>             .HasMany(p =&gt; p.Tags)<br>             .WithMany(p =&gt; p.Posts)<br>             .UsingEntity(j =&gt; j.ToTable(&quot;PostTagsTest&quot;));</pre><p>There’s also <strong>another way</strong>. You can create a class for this table at first. These are what the classes should look like:</p><pre>    public class Post<br>    {<br>        public int PostId { get; set; }<br>        public string Title { get; set; }<br>        public string Content { get; set; }<br>        public ICollection&lt;Tag&gt; Tags { get; set; }<br><br>        public List&lt;PostTag&gt; PostTags { get; set; }<br>    }<br>   <br>   public class Tag<br>  {<br>    public int TagId { get; set; }<br>    public ICollection&lt;Post&gt; Posts { get; set; }<br>    public List&lt;PostTag&gt; PostTags { get; set; }<br>   }<br>    public class PostTag<br>    {<br>      public Post Post { get; set; }<br>      public int PostId { get; set; }<br><br>      public int TagId { get; set; }<br>      public Tag Tag { get; set; }<br><br>      public  DateTime AddedOn { get; set; }<br><br>    }</pre><p>Don’t forget to add the configuration using Fluent API.</p><pre>    modelBuilder.Entity&lt;Post&gt;()<br>                .HasMany(p =&gt; p.Tags)<br>                .WithMany(p =&gt; p.Posts)<br>                .UsingEntity&lt;PostTag&gt;(<br>                     j=&gt;j<br>                       .HasOne(pt=&gt;pt.Tag)<br>                       .WithMany(t=&gt;t.PostTags)<br>                       .HasForeignKey(pt=&gt;pt.TagId),<br>                    j=&gt;j<br>                      .HasOne(pt=&gt;pt.Post)<br>                      .WithMany(pt=&gt;pt.PostTags)<br>                      .HasForeignKey(pt=&gt;pt.PostId),<br>                    j =&gt;<br>                    {<br>                        j.Property(pt =&gt; pt.AddedOn).HasDefaultValueSql(&quot;GETDATE()&quot;);<br>                        j.HasKey(t =&gt; new { t.PostId, t.TagId });<br>                    }<br><br>                 <br>                     );</pre><p>But, what does all of that mean?</p><p>This allows us to use <strong>PostTag </strong>to join the <strong>Post</strong> and <strong>Tag</strong> tables:</p><pre>UsingEntity&lt;PostTag&gt;</pre><p>Now, we need to configure the <strong>PostTag -Tag</strong> relationship:</p><pre> j=&gt;j<br>   .HasOne(pt=&gt;pt.Tag)<br>   .WithMany(t=&gt;t.PostTags)<br>   .HasForeignKey(pt=&gt;pt.TagId),</pre><p>This means that each Tag can have many PostTags and they are connected via the <strong>TagId</strong>.</p><p>The same concept is implemented for the <strong>PostTag</strong> — <strong>Post</strong> relationship:</p><pre> j=&gt;j<br>   .HasOne(pt=&gt;pt.Post)<br>   .WithMany(pt=&gt;pt.PostTags)<br>   .HasForeignKey(pt=&gt;pt.PostId),</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*3gJxFwjbs8IyZVBgQDXL7w.gif" /></figure><p>To create a <strong>composite primary key</strong> made up of the PostId and TagId, and get the <strong>date automatically</strong>:</p><pre> j =&gt;<br> {<br>     j.Property(pt =&gt; pt.AddedOn).HasDefaultValueSql(&quot;GETDATE()&quot;);<br>     j.HasKey(t =&gt; new { t.PostId, t.TagId });<br> }</pre><p>It might seem a bit complex at first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*psK4w2hbTPAYQfKjTHpcaA.gif" /></figure><p>However, there’s <strong>another way</strong>.</p><p>Comment out the last the configuration. Replace it with this:</p><pre> modelBuilder.Entity&lt;PostTag&gt;()<br>         .HasKey(t =&gt; new { t.PostId, t.TagId});<br> modelBuilder.Entity&lt;PostTag&gt;()<br>             .HasOne(pt =&gt; pt.Post)<br>             .WithMany(p =&gt; p.PostTags)<br>             .HasForeignKey(pt =&gt; pt.PostId);<br> modelBuilder.Entity&lt;PostTag&gt;()<br>             .HasOne(pt =&gt; pt.Tag)<br>             .WithMany(p =&gt; p.PostTags)<br>             .HasForeignKey(pt =&gt; pt.TagId);</pre><p>Don’t forget to remove these if you use this configuration:</p><pre>   public ICollection&lt;Tag&gt; Tags { get; set; }</pre><pre>  public List&lt;PostTag&gt; PostTags { get; set; }</pre><p>Why did we do this? Because, this way doesn’t explicitly connect these two tables together. It is not direct.</p><p>That wraps up today’s Entity Framework journal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*DP2AzHyBTwRvh7cXwbQj2g.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2f93dd4ac21" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 4: Tables in Entity Framework]]></title>
            <link>https://medium.com/@farahanany17/day-4-tables-in-entity-framework-a6372a73a1bc?source=rss-f7630ce64b7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6372a73a1bc</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <dc:creator><![CDATA[Farah Anany]]></dc:creator>
            <pubDate>Wed, 17 Dec 2025 09:06:01 GMT</pubDate>
            <atom:updated>2025-12-17T10:18:26.552Z</atom:updated>
            <content:encoded><![CDATA[<p>Remember your Database Course that you might have taken in Uni? Well, that might come in handy right now. Today is all about customizing your tables and working on them by using EF’s full potential.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*h33swKUhQGZmkk8c1SU1zg.gif" /><figcaption>Can’t miss the chance to make use a reference from the Office.</figcaption></figure><h4>Changing the Column’s Name</h4><p>How do we change the column name so that its name in the class is not the same as the database? It is so simple that it’s just one line. In the <strong><em>OnModelCreating</em></strong> method, use this line of code:</p><pre><br>        modelBuilder.Entity&lt;Blog&gt;()<br>            .Property(b =&gt; b.Url)<br>            .HasColumnName(&quot;BlogUrl&quot;);<br>    </pre><h4>Data Types in Database Columns</h4><p>In the <em>Blog </em>class , add the Rating property:</p><pre>  public decimal Rating { get; set; }</pre><p>Then, we are then able to choose how the data will be stored in the database (specifically which data type):</p><pre> modelBuilder.Entity&lt;Blog&gt;(eb =&gt;<br> {<br>     eb.Property(b =&gt; b.Url).HasColumnType(&quot;varchar(200)&quot;);<br>     eb.Property(b =&gt; b.Rating).HasColumnType(&quot;decimal(5,2)&quot;);<br> });</pre><p>The Url can store up to 200 characters and the Rating will be stored as a number that will have up to 5 digits before the decimal and 2 digits after the decimal.</p><h4>Giving a Property a Maximum Length</h4><pre>  modelBuilder.Entity&lt;Blog&gt;()<br>               .Property(b =&gt; b.Url)<br>               .HasMaxLength(200);</pre><h4>Adding Comments to Columns</h4><p>But, hey! Why do we need to add comments in the first place?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*B222PElO4urjDgAPrj3_kQ.gif" /><figcaption>Why??</figcaption></figure><p>For <strong>documentation</strong> purposes, these comments can help remind us of the purposes of these columns or help us mark important notes regarding these columns.</p><p>How do you do it?</p><pre> modelBuilder.Entity&lt;Blog&gt;()<br>              .Property(b =&gt; b.Url)<br>              .HasComment(&quot;This is a sample comment&quot;);</pre><h4>Primary Keys</h4><p>Let’s add a class in the Model Folder and call it <strong><em>Book</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mekppG9rczea298J8I9FuQ.gif" /><figcaption>SO MANY BOOOOOOOOKS!</figcaption></figure><pre>  public class Book<br>  {<br>      public int Id { get; set; }<br>      public string Name { get; set; }<br>      public string Author { get; set; }<br>  }</pre><p>Don’t forget to add the table to the <strong><em>ApplicationDbContext</em></strong> class:</p><pre>public DbSet&lt;Book&gt; Books { get; set; }</pre><p>Then, add a migration: <strong><em>add-migration addBooksTable</em></strong></p><p>Now, what if we want assign the primary key to a specific property?</p><pre> modelBuilder.Entity&lt;Book&gt;()<br>      .HasKey(b =&gt; b.BookKey);</pre><p>This will do the trick. Now, your primary key is BookKey. Just don’t forget to add it to the Book’s properties. The primary key is always unique.</p><p>How do I change the primary key’s name? Just add a little extra something to our chain:</p><pre>modelBuilder.Entity&lt;Book&gt;()<br>      .HasKey(b =&gt; b.BookKey)<br>      .HasName(&quot;New_PrimaryKey&quot;);</pre><h4>Composite Keys</h4><p>Composite keys are made up of two or more properties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cCShNvgxkMxi4FSKVZJGNA.jpeg" /></figure><p>Sometimes, we are required to use composite keys and to do so this is all you need:</p><pre> modelBuilder.Entity&lt;Book&gt;()<br>      .HasKey(b =&gt; new {b.Name,b.Author});</pre><h4>Setting a Default Value</h4><pre>  modelBuilder.Entity&lt;Blog&gt;()<br>       .Property(b =&gt; b.Rating)<br>       .HasDefaultValue(1);</pre><h4>Using SQL Queries as a Default Value</h4><p>If the person does not enter the date that the post was submitted on, it automatically gets generated using SQL.</p><pre>modelBuilder.Entity&lt;Blog&gt;()<br>      .Property(b =&gt; b.SubmittedOn)<br>      .HasDefaultValueSql(&quot;GETDATE()&quot;);</pre><h4>Computed Columns</h4><p>Create a class called <strong><em>Author</em></strong>:</p><pre> public class Author<br> {<br>     public int Id { get; set; }<br>     public string FirstName { get; set; }<br>     public string LastName { get; set; }<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*BJ6T7Ma7GjXrsFF7YGf_lw.gif" /><figcaption>Class Author is Created!</figcaption></figure><p>Add it to the <strong><em>ApplicationDbContext.</em></strong></p><pre>public DbSet&lt;Author&gt; Authors { get; set; }</pre><p>Make the last and first names required.</p><pre> modelBuilder.Entity&lt;Author&gt;()<br>        .Property(a =&gt; a.FirstName)<br>        .IsRequired();<br> modelBuilder.Entity&lt;Author&gt;()<br>        .Property(a =&gt; a.LastName)<br>        .IsRequired();</pre><p>Add migration. Then, update database.</p><p>Let’s try it out.</p><pre> static void Main(string[] args)<br> {<br>     var _context = new ApplicationDbContext();<br>     var author = new Author<br>     {<br>      FirstName = &quot;Farah&quot;,<br>      LastName = &quot;Anany&quot;,<br>     };<br>     _context.Authors.Add(author);<br>     _context.SaveChanges();<br> <br> }</pre><p>Voila!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1xIjr36GL4e3ZHFeH3NBEg.png" /></figure><p>Now, let’s get the computed column:</p><p>We need to create a property called DisplayName</p><pre>public string DisplayName { get; set; }</pre><p>Then, we are going to make it required, give it a maximum length and compute it!</p><pre> modelBuilder.Entity&lt;Author&gt;()<br>       .Property(a =&gt; a.DisplayName)<br>       .HasMaxLength(150)<br>       .IsRequired();<br><br> modelBuilder.Entity&lt;Author&gt;()<br>     .Property(p =&gt; p.DisplayName)<br>     .HasComputedColumnSql(&quot;[LastName]+&#39;,&#39;+[FirstName]&quot;);</pre><p>Add a migration and update the database.</p><h4>Primary Key Default Value</h4><p>Create a class called <strong><em>Category</em></strong> in the folder Models</p><pre> public class Category<br> {<br>     public byte Id { get; set; }    <br>     public string Name { get; set; }<br> }</pre><p>Add it to <strong><em>ApplicationDbContext</em></strong> class</p><pre> public DbSet&lt;Category&gt; Categories { get; set; }</pre><p>In the OnModelCreating method, make the name required and give it a sample maximum length.</p><pre><br>            modelBuilder.Entity&lt;Category&gt;()<br>                 .Property(n =&gt; n.Name)<br>                 .HasMaxLength(100)<br>                 .IsRequired();</pre><p>It is ludicrous for all the records to have the same primary key. How will they be identified? And, how do we fix this problem?</p><pre>modelBuilder.Entity&lt;Category&gt;()<br>      .Property(c =&gt; c.Id)<br>      .ValueGeneratedOnAdd();</pre><p>Let’s try it out in the Program class.</p><pre> var _context = new ApplicationDbContext();<br> var category = new Category<br> {<br>     Name = &quot;Test&quot;<br> };<br> _context.Categories.Add(category);<br> _context.SaveChanges();</pre><p>It should work and generate Ids each time you run the program. If you hadn’t added this last time, you would have found this exception:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6TuQAangauTXFiGH-3rlmg.png" /></figure><p>But, wait we’ve got an error. This is what the console says:</p><p>To change the IDENTITY property of a column, the column needs to be dropped and recreated.</p><p>To avoid this hassle, add the preceding code when creating the table before applying the migration. This is because we can not change the Id when it already exists in the table</p><p>But, now that we have this problem at hand, don’t forget to delete the unwanted migrations from the Migrations Folder and the old tables.</p><p>That’s it for today. Thank you…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6372a73a1bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 3: Domain Models and More]]></title>
            <link>https://medium.com/@farahanany17/day-3-domain-models-and-more-433a3500d53e?source=rss-f7630ce64b7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/433a3500d53e</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[fluent-api]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[domain-model]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Farah Anany]]></dc:creator>
            <pubDate>Tue, 16 Dec 2025 10:26:27 GMT</pubDate>
            <atom:updated>2025-12-16T10:26:27.304Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>All roads lead to Rome.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EHsoXKhFybtIBTJgM7mnOQ.jpeg" /></figure><p>This applies to so many cases in the realm of coding. While I was studying, I realized that some things could be made using two methods; Data Annotation and Fluent API. In my examples, I am using Fluent API. But, why is that? It is because allows for more control and flexibility when configuring the needed models.</p><h4>1. Three Ways to Work with Domain Models in Entity Framework</h4><ol><li>Let’s create a class and call it <strong><em>Post</em></strong> and put it inside the <em>Models folder</em> that we created earlier. It should have four properties: <strong>Id</strong>, <strong>Title</strong>, <strong>Content</strong>, and <strong>Blog</strong>. Just like a real post! Blog here is now connected to Post.</li></ol><pre> public class Post<br> {<br>     public int Id { get; set; }<br>     public string Title { get; set; }<br>     public string Content { get; set; }<br>    //navigation property<br>     public Blog Blog { get; set; }<br><br> }</pre><p>2. Then, inside our Blog class, add a new property. The class should look like this:</p><pre> public class Blog<br> {<br>     public int Id { get; set; }<br>     public string Url { get; set; }<br><br>     //new thing<br>     public List &lt;Post&gt; Posts { get; set; }<br><br> }</pre><p>This way every Blog could have many Posts!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*jLOlHPTxra9PxMIZxb5Yuw.gif" /></figure><p>Now, use the command <strong>add-migration addPosts</strong><em>. </em>You are able to see in the new migration that the Posts are connected to the Blog via foreign key.</p><p>If you look closely at the <strong>new migration</strong>, you’ll find this line:</p><pre>onDelete: ReferentialAction.Cascade</pre><p>This means that if a parent is deleted, its children are also deleted . It’s better to change the word <em>Cascade</em> to <strong><em>Restrict</em></strong> to prevent deleting entities that have children. The child must be deleted first, then the parent.</p><p>Create a class and call it AuditEntry and put in the Models Folder. This is what it should look like:</p><pre> public class AuditEntry<br> {<br>     public string Id {  get; set; }<br>     public string Username { get; set; }<br>     public string Action { get; set; }<br> }</pre><p>But, how can we make it a domain class?</p><p>The answer is pretty straightforward. Remember the <em>OnModelCreating</em> function in the <em>ApplicationDbContext</em> class? Yes, we are going to use this function a lot today. After adding this line, AuditEntry has now become a Domain Class.</p><pre> modelBuilder.Entity&lt;AuditEntry&gt;();</pre><p>Now, remove the migration, because we don’t want to apply the last change to the database and <strong>add-migration addPosts</strong> again. You will find that two tables have been created: <strong>AuditEntry </strong>and <strong>Post</strong>.</p><p>To sum things up, there are three ways approaches to defining domain models in EF Core are:</p><ol><li>Using DbSet</li></ol><pre> public DbSet&lt;Blog&gt; Blogs { get; set; }</pre><p>2. Using navigation property</p><pre>public Blog Blog { get; set; }</pre><p>3. Using modelBuilder</p><pre>modelBuilder.Entity&lt;AuditEntry&gt;();</pre><h4>2. Excluding Entities from Migrations and Database Schema in Entity Framework Core</h4><p>Firstly, do not forget remove the last migration because we don’t need it in this scenario</p><p>In the <strong><em>OnModelCreating</em></strong><em> </em>function in <strong><em>ApplicationDbContext</em></strong> class, use this line of code:</p><pre>modelBuilder.Ignore&lt;Post&gt;();</pre><p>Then, use <strong><em>add-migration RemovePosts</em></strong> for example to see the changes. Remove the migration to undo. This ensures Post is excluded from the database schema or the migration. This completely removes the Post from the EF Core model.</p><p>The following piece of code keeps the Blog in the EF Core and map Blog to the Blogs table. However, it will be excluded from Migrations.</p><pre><br>      modelBuilder.Entity&lt;Blog&gt;()<br>                  .ToTable(&quot;Blogs&quot;, b =&gt; b.ExcludeFromMigrations());<br>  </pre><h4>3. How Do I Change a Table’s Name?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*dwARtefIrJDIS4XxVrmrww.gif" /><figcaption>No, it’s not. We can still change it.</figcaption></figure><p>What if we have a class with a certain name and we want a table of this class to be mapped to database with a different name? That is absolutely fine.</p><p>Use the command <strong><em>add-migration changeTableTitle. </em></strong>Type in the following code:</p><pre> modelBuilder.Entity&lt;Post&gt;().ToTable(&quot;MyPosts&quot;);</pre><p>The last code was used in Step 2 , but it was excluded from migrations.</p><h4>4. Choosing the Schema</h4><p>I know we have been using the <strong><em>modelBuilder</em></strong><em> </em>parameter in the ApplicationDbContext class a lot today, but it’s going to be our right hand man.</p><p>To change the schema, you need two parameters: the name of the table and the name of the schema</p><pre><br>  modelBuilder.Entity&lt;Post&gt;().ToTable(&quot;Posts&quot;,schema:&quot;blogging&quot;);<br>  </pre><p>But, how do we avoid assigning blogging as a schema each time and make it our default schema? You can do so using this line:</p><pre>modelBuilder.Entity&lt;Post&gt;().HasDefaultSchema(&quot;blogging&quot;);</pre><h4>5. Mapping a Domain Model to a View</h4><p>In order to map a domain model to a view, you need two parameters: the name of the domain model and the view (obviously).</p><pre>modelBuilder.Entity&lt;Post&gt;().ToView(&quot;SelectPosts&quot;,schema:&quot;blogging&quot;);</pre><h4>6. Excluding Properties</h4><p>In the <em>Blog</em> class, add a new public property called SubmittedOn :</p><pre>     public DateTime SubmittedOn { get; set; }</pre><p>Add this line of code to the OnModelCreating function:</p><pre> modelBuilder.Entity&lt;Blog&gt;().Ignore(b =&gt; b.SubmittedOn);</pre><p>Use <strong><em>add-migration SumbittedOnToBlogs. </em></strong>If you actually apply the migration, use the<strong><em> update-database command.</em></strong></p><p>But, why would we exclude properties? Sometimes, we need to compute things and these things are derived properties. In other cases, they are just temporary. And, those are just a couple of examples.</p><p>Today, we learned how to work with domain models and change things that are concerned with our schema. Next time, we will be more in depth in the world of Entity Framework.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=433a3500d53e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 2: Migration Rollback & Fluent API]]></title>
            <link>https://medium.com/@farahanany17/day-2-migration-rollback-fluent-api-basics-5a586fdb666a?source=rss-f7630ce64b7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a586fdb666a</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[entity-framework]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <dc:creator><![CDATA[Farah Anany]]></dc:creator>
            <pubDate>Fri, 12 Dec 2025 19:42:33 GMT</pubDate>
            <atom:updated>2025-12-12T19:44:14.339Z</atom:updated>
            <content:encoded><![CDATA[<p>Ooookay, so staying consistent with this is no cake walk but determination is key. This is a continuation of the code we started on Day 1. This day is the day I really got a feel of how migrations operate and how the console is integral to getting things done. It is important to avoid deleting things manually and use commands instead. Needless to say, organizing things in folders is crucial especially as the code gets bigger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*r_hRuLq9yatCijGUDUBuxw.gif" /></figure><p>If you want to go back to the first stage, don’t delete it manually. You must use the Package Manager Console. Use this command: <strong>update-database –migration:0, </strong>then <strong>remove-migration</strong></p><h4>1. Seeding Data with Darth Vader (and Friends)</h4><p>Firstly, we need to add a migration. In this example, I decided to call it fillinData. Command: <strong><em>add-migration fillinData</em></strong></p><p>Use the parameter migrationBuilder and Sql function→ we want to insert/delete employees in the table . As we said before, <strong>Up is responsible for inserting and Down is concerned with deleting records</strong>:</p><pre>protected override void Up(MigrationBuilder migrationBuilder)<br>{<br>    migrationBuilder.Sql(&quot;INSERT INTO Employees VALUES (&#39;Darth Vader&#39;)&quot;);<br>}<br><br>protected override void Down(MigrationBuilder migrationBuilder)<br>{<br>    migrationBuilder.Sql(&quot;DELETE FROM Employees WHERE Name=&#39;Darth Vader&#39;&quot;);<br>}</pre><p>Now, you have an employee called Darth Vader.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Y5KSM0zIk7RMMpE3Tacsw.jpeg" /></figure><p>Then run:</p><p><strong><em>update-database</em></strong></p><p>to actually implement and see everything in action.</p><p>If you want to delete the <strong>last migration</strong>, you have to go back to the last state.</p><p>Go back to the last state by using this command:</p><p><strong><em>update-database &lt;name-of-the-last-state&gt;</em></strong></p><p>Then, use this command to delete the last migration:</p><p><strong><em>Remove-Migration</em></strong></p><h4>2.Creating Your First Blog Table</h4><ul><li>Okay now, we’re going to kick things up a notch. First of all, delete the <strong>Employee</strong> class and the associated code in the <strong>Program</strong> class and the <strong>ApplicationDbContext</strong> class.</li><li>For organizational purposes, create a folder called <strong>Models</strong> and inside of it, create a class called <strong>Blog</strong>.</li><li>Give it an <strong>Id</strong> and a <strong>Url</strong>.</li><li>In the <strong>ApplicationDbContext</strong>, add this code. This represents a collection of entities of type Blog (that’s the model we just created):</li></ul><pre>public DbSet&lt;Blog&gt; Blogs { get; set; }</pre><p>Don’t forget to import the Models so we can use the Blog class:</p><pre>using Projects.Models;</pre><p>Now, use the command:</p><p><strong><em>add-migration AddBlogs</em></strong></p><p>Then, use the command:</p><p><strong><em>update-database</em></strong></p><p>Now, the <strong>Blog</strong> table has been successfully created 🎉</p><h4>3.Making Columns Required with Fluent API</h4><p>In the <strong>ApplicationDbContext </strong>class, we use Fluent API to make the <strong>Url</strong> column a required field:</p><pre>protected override void OnModelCreating(ModelBuilder modelBuilder)<br>{<br>    modelBuilder.Entity&lt;Blog&gt;()<br>        .Property(m =&gt; m.Url)<br>        .IsRequired();<br>}</pre><p>Now, we need to use the command:</p><p><strong><em>add-migration UpdateBlog</em></strong></p><h4>4. Configurations: The Secret to Staying Organized</h4><p>Because the code can get so big in real projects, we tend to create multiple folders in order to organize things.</p><p>Create a folder. Let’s call it <strong>Configurations</strong>. Add a class inside this folder and call it <strong>BlogConfig</strong>.</p><p>This class will implement the IEntityTypeConfiguration interface and give it a type Blog like so: BlogConfig : IEntityTypeConfiguration&lt;Blog&gt;</p><p>Don’t forget all the imports:</p><pre>using Microsoft.EntityFrameworkCore;<br>using Microsoft.EntityFrameworkCore.Metadata.Builders;<br>using Projects.Models; // Because the model we want to use is in the folder called Models</pre><p>Now, create a function called <strong>Configure</strong> and make it public and void. This function will be called later on in the ApplicationDbContext class to replace what’s inside OnModelCreating. This is because later on, we might need to add more models using Fluent API and it’s better for organization.</p><p>This is how the <strong>BlogConfig</strong> class should look:</p><pre>public class BlogConfig : IEntityTypeConfiguration&lt;Blog&gt;<br>{<br>    public void Configure(EntityTypeBuilder&lt;Blog&gt; builder) <br>    {<br>        builder.Property(m =&gt; m.Url)<br>            .IsRequired();<br>    }<br>}</pre><p>Remember the OnModelCreating function in the ApplicationDbContext class? Replace the code inside it with this:</p><pre>new BlogConfig().Configure(modelBuilder.Entity&lt;Blog&gt;());</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*L-3O7-ELJUH6cxRj8j1v-w.gif" /></figure><p>The final <strong>ApplicationDbContext</strong> class should look like this:</p><pre>public class ApplicationDbContext : DbContext<br>{<br>    protected override void OnConfiguring(DbContextOptionsBuilder options) =&gt;<br>        options.UseSqlServer(@&quot;Data Source=(localdb)\ProjectModels;Initial Catalog=EFCore;Integrated Security=True;&quot;);<br><br>    protected override void OnModelCreating(ModelBuilder modelBuilder)<br>    {<br>        new BlogConfig().Configure(modelBuilder.Entity&lt;Blog&gt;());<br>    }<br><br>    public DbSet&lt;Blog&gt; Blogs { get; set; }<br>}</pre><p>Now that everything is neat and tidy, don’t forget to use the <strong><em>add-migration</em></strong> command then the <strong><em>update-database</em></strong>.</p><p>That’s it for today’s EF journal. Be sure to check out the next part!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*R7lgiS6bl7y4X1xPq0GVvQ.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a586fdb666a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 1: Entity Framework Basics]]></title>
            <link>https://medium.com/@farahanany17/day-1-entity-framework-basics-7144b5fce52e?source=rss-f7630ce64b7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7144b5fce52e</guid>
            <category><![CDATA[sql-server]]></category>
            <category><![CDATA[entity-framework]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Farah Anany]]></dc:creator>
            <pubDate>Thu, 11 Dec 2025 22:09:50 GMT</pubDate>
            <atom:updated>2025-12-11T23:32:25.191Z</atom:updated>
            <content:encoded><![CDATA[<p>Learning a new framework can either be a fun ride or a soul-draining journey. Before I started diving into this, I was honestly super apprehensive. However, as I started reading some articles online and watching some tutorials on YouTube, I came to realize that it is not that difficult. The key here is to take it one step at a time and LOTS AND LOTS OF PRACTICE. And, because I don’t like to gatekeep and believe that reading documentation can be faster than watching an endless amount of videos, I thought it would be great to document my Entity Framework learning journey.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*4YOfDDzaj4kcFfwEa6DjnQ.jpeg" /></figure><h4>Step 1: Connecting to the SQL Server</h4><ol><li>Create a new project and choose <strong>Console App (.NET Core)</strong></li><li>Name it <strong>EFCore</strong> for this example</li><li>Install the needed packages from <strong>Tools. </strong>You can also download them using the Package Manager Console. But, I thought this would just be easier at first. I’m using SQL in this exmaple.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*uzFO-7PLH3HSYg5-Ymz6Fw.png" /><figcaption>Needed Downloads</figcaption></figure><h4>Step 2: Creating our First Class</h4><ol><li>Add a class and call it <em>ApplicationDbContext</em>. This class is going to be the bridge between C# and our database. It will inherit from <em>DbContext</em>.</li><li>The class should look similar to this:</li></ol><pre>public class ApplicationDbContext: DbContext<br> {<br>     protected override void OnConfiguring(DbContextOptionsBuilder options)=&gt;<br>        <br>         options.UseSqlServer(@&quot;Data Source=(localdb)\ProjectModels;Initial Catalog=EFCore;Integrated Security=True;&quot;);<br>        <br> }</pre><p>3. But what exactly is this piece of code? In order to create this connection, after inheriting from <em>DbContext</em>, we must override the <strong>OnConfiguring</strong> method. Use <em>UseSqlServer</em> in order to choose the database that you wanna include in your project. This takes a connection string. But what is that? And where do we get it?</p><p>4. In order to get the connection string, you have to get <strong>SQL Server Object Server. </strong>It’s either already in your view or you can look it up in your Search bar<strong>.</strong></p><p>5. Right click on the database in your SQL Server Object Server. Click on properties and in the pop up of the SQL Server Instance, you’ll find the connection string. Copy and paste it to give it to the <em>UseSqlServer</em>. Don’t forget to change the name of the Initial Catalog (which is the name of the database) to make it your own.</p><p>6. Don’t forget the <strong>@</strong> decorator before the connection string</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*ovk2uBcobMUO1ElfoqJ8sQ.png" /></figure><h4>Step 3: Using commands in the Package Manager Console and creating a table</h4><ol><li>Create a new class and name it Employee and give it an ID and a Name with setters and getters:</li></ol><pre>  public class Employee<br>  {<br>      public int Id { get; set; }<br>      public string Name { get; set; }<br>  }</pre><p>2. Open the <strong>Package Manager Console</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pwOiTAEMY4WISdOl055WaQ.png" /><figcaption>This is where we type in our commands.</figcaption></figure><p>3. Type in the command to create a new migration: add-migration InitialCreate</p><ul><li>The migration has 2 functions: <strong>Up</strong> and <strong>Down</strong></li><li><strong>Up</strong> → is concerned with updates</li><li><strong>Down</strong> → if you want to go back to previous versions</li></ul><p>4. Now, use the command if you want to remove the the migration: remove-migration</p><ul><li>To see what will happen to the migration, we need to make changes.</li></ul><p>5.Add this piece of code to our <em>ApplicationDbContext </em>class. What this line does is use the Model Employee that we created earlier to create a table of this type :</p><pre>public DbSet&lt;Employee&gt; Employees { get; set; }</pre><p>6. Now, for instance, use the command add-migration InitialCreate. Then, use update-database to see the changes.</p><h4>Step 4: Filling in the Table</h4><ol><li>In the <em>Program</em> class, we must create an instant of the type <em>ApplicationDbConext. </em>Okay, but what’s up with the _ before the var context? It’s just one of the known naming conventions. It is not integral to the execution of the code.</li></ol><pre>var _context = new ApplicationDbContext();</pre><p>2. Create an object of the domain model [Employee] and call it employee for example. Yes, very creative. I know. I chose Lewis Carroll as the first employee. I should’ve created a table of authors but let’s pretend that this dude is an employee in some parallel universe for the sake of this example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*mM-2eZxFfGFL6HkM8dNqMg.jpeg" /><figcaption>One of the Greatest Authors and Mathematicians of ALL TIME!</figcaption></figure><p>3. But hey, where is the ID? We didn’t give the employee an ID, because it is a primary key that will be automatically generated. Completely Unique.</p><pre>var employee=new Employee {Name=&quot;Lewis Carroll&quot;};</pre><p>4. Now, we have to add the employee that we just created to our database</p><pre>_context.Employees.Add(employee);</pre><p>5. Don’t forget to save the changes so the update is reflected in our database!</p><pre>_context.SaveChanges();</pre><p>Congratulations! You’ve now created your first table and you’ve got your first employee.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*KowWujusMfDjlkjeeOFEBg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7144b5fce52e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>