<?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 Imashi Dharmawardana on Medium]]></title>
        <description><![CDATA[Stories by Imashi Dharmawardana on Medium]]></description>
        <link>https://medium.com/@imashidharmawardana?source=rss-fec5d42eaf43------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0wT-fXnDUSkFFuJQ.jpg</url>
            <title>Stories by Imashi Dharmawardana on Medium</title>
            <link>https://medium.com/@imashidharmawardana?source=rss-fec5d42eaf43------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 22:06:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@imashidharmawardana/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[Code-First vs. Database-First in Entity Framework: Which One is Right for You?]]></title>
            <link>https://medium.com/@imashidharmawardana/code-first-vs-database-first-in-entity-framework-which-one-is-right-for-you-801c7d65307c?source=rss-fec5d42eaf43------2</link>
            <guid isPermaLink="false">https://medium.com/p/801c7d65307c</guid>
            <category><![CDATA[entity-framework]]></category>
            <category><![CDATA[code-first]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[database-first]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Imashi Dharmawardana]]></dc:creator>
            <pubDate>Tue, 11 Mar 2025 10:35:04 GMT</pubDate>
            <atom:updated>2025-03-11T10:36:59.865Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/955/1*E9l1_sbuv1praQAdYiACfw.png" /></figure><p>When working with <strong>Entity Framework (EF)</strong> in .NET, one of the first decisions developers face is choosing between <strong>Code-First</strong> and <strong>Database-First</strong> approaches. Each has its strengths, and the right choice depends on your project’s needs.</p><p>🔍 <strong>Understanding Entity Framework (EF)</strong></p><p>EF is an <strong>Object-Relational Mapper (ORM)</strong> that simplifies data access by allowing developers to work with <strong>C# objects instead of SQL queries</strong>. It bridges the gap between relational databases and object-oriented programming.</p><p>Now, let’s explore the two primary approaches to working with EF.</p><p>📊 <strong>Database-First Approach: Start with an Existing Database</strong></p><p>The <strong>Database-First</strong> approach is ideal when your database schema is already designed, or you’re working with an existing system. In this case, EF generates entity models and a <strong>Db Context</strong> from the database.</p><h3>When to Choose Database-First:</h3><p>✔ You already have a well-structured database.<br> ✔ Your project requires <strong>database-first design</strong> with minimal changes to the schema.<br> ✔ You are integrating with <strong>legacy databases</strong>.<br> ✔ Your team prefers database-defined rules and constraints.</p><h3>How to Implement Database-First:</h3><p>1️. <strong>Use an existing database</strong> or create a new one.<br>2️. <strong>Create a .NET project</strong> in Visual Studio.<br>3️. <strong>Install Entity Framework Core via NuGet:</strong></p><pre>Install-Package Microsoft.EntityFrameworkCore.SqlServer<br>Install-Package Microsoft.EntityFrameworkCore.Tools</pre><p>4. <strong>Generate Models &amp; Db Context from Database</strong>:</p><pre>Scaffold-DbContext &quot;Your_Connection_String&quot; Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models</pre><p>5. <strong>Use the generated Db Context in your application</strong>:</p><pre>using (var context = new YourDbContext())<br>{<br>    var users = context.Users.ToList();<br>    Console.WriteLine($&quot;Total Users: {users.Count}&quot;);<br>}</pre><p>🛠 <strong>Code-First Approach: Define the Models in C#</strong></p><p>If you’re building a new application <strong>from scratch</strong>, the <strong>Code-First</strong> approach gives you full control. Here, the database is generated <strong>from your C# classes</strong>, and changes are managed through <strong>migrations</strong>.</p><p>✅ When to Choose Code-First:</p><p>✔ You’re starting fresh and want to define the database in <strong>C# models</strong>.<br> ✔ You need full flexibility over the schema.<br> ✔ You prefer <strong>version control for database changes</strong> using migrations.<br> ✔ You want to <strong>avoid database dependency</strong> while developing.</p><h3>⚙ How to Implement Code-First:</h3><p>1️. <strong>Create a .NET project</strong> in Visual Studio.<br>2️. <strong>Install Entity Framework Core</strong> (same as Database-First).<br>3️. <strong>Define your entity classes</strong>:</p><pre>public class User<br>{<br>    public int Id { get; set; }<br>    public string Name { get; set; }<br>}</pre><p>4. <strong>Create a Db Context class</strong>:</p><pre>public class AppDbContext : DbContext<br>{<br>    public DbSet&lt;User&gt; Users { get; set; }<br><br>    protected override void OnConfiguring(DbContextOptionsBuilder options)<br>        =&gt; options.UseSqlServer(&quot;Your_Connection_String&quot;);<br>}</pre><p>5. <strong>Run Migrations &amp; Create the Database</strong>:</p><pre>Add-Migration InitialCreate<br>Update-Database</pre><p>6. <strong>Interact with the database</strong> using EF:</p><pre>using (var context = new AppDbContext())<br>{<br>    context.Users.Add(new User { Name = &quot;Alice&quot; });<br>    context.SaveChanges();<br>}</pre><h3>💡 Final Thoughts:</h3><p>🔹 <strong>Choose Code-First</strong> if you want flexibility and control over your schema.<br> 🔹 <strong>Choose Database-First</strong> if you’re working with an existing database.</p><p>Both approaches have their place in real-world applications. The key is to choose the one that aligns with your <strong>project needs, team expertise, and long-term goals</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=801c7d65307c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>