<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fred Chuks</title>
    <description>The latest articles on DEV Community by Fred Chuks (@fredchuks).</description>
    <link>https://dev.to/fredchuks</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1707479%2Ffbed70ff-b262-4210-bd9c-0ed5a0ce8de8.png</url>
      <title>DEV Community: Fred Chuks</title>
      <link>https://dev.to/fredchuks</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fredchuks"/>
    <language>en</language>
    <item>
      <title>SQL Database Migration in .NET with Entity Framework Core</title>
      <dc:creator>Fred Chuks</dc:creator>
      <pubDate>Sun, 30 Jun 2024 19:34:42 +0000</pubDate>
      <link>https://dev.to/fredchuks/sql-database-migration-in-net-with-entity-framework-core-3117</link>
      <guid>https://dev.to/fredchuks/sql-database-migration-in-net-with-entity-framework-core-3117</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Recently, I had an issue with migration in .NET using entity framework core. I kept getting error reports on the NuGet package manager console. This article would provide a detailed explanation of how I resolved this problem. I am Fredrick Chukwuma, a .NET Developer that is result and process-oriented.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Entity Framework?
&lt;/h2&gt;

&lt;p&gt;Entity Framework is a modern object-relation mapper that lets you build a clean, portable, and high-level data access layer with .NET (C#) across a variety of databases. It makes programming easier and faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow along, ensure you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Visual studio&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Familiarity with .NET and the Entity framework&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Installed .NET locally&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A SQL Server or Azure SQL database&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Create a new ASP.NETCore Web API Project
&lt;/h2&gt;

&lt;p&gt;Open visual studio, click on new project, select ASP.NETCore Web API and configure the project&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80t5udbua0yr3jsbt26l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80t5udbua0yr3jsbt26l.png" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80t5udbua0yr3jsbt26l.png" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Create a Model
&lt;/h2&gt;

&lt;p&gt;Create a new folder named &lt;em&gt;"Models"&lt;/em&gt; in the root directory. Then create a new c# class named &lt;em&gt;"Student.cs"&lt;/em&gt; and add properties as done below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class Student
    {
        public Guid Id { get; set; }
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public string? Department { get; set; }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The question mark at the end of each property data type is to make it nullable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Include connection string in &lt;em&gt;appsettings.json&lt;/em&gt; file
&lt;/h2&gt;

&lt;p&gt;Open the &lt;em&gt;appsettings.json&lt;/em&gt; file then add the line below. Replace the server name with the name of your server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"ConnectionStrings": { "DefaultConnection": "Server=Your-ServerName\\SQLEXPRESS;Database=StudentApp;Trusted_Connection=True;TrustServerCertificate=True" }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 4: Create and Register your App DbContext
&lt;/h1&gt;

&lt;p&gt;Create a folder named &lt;em&gt;Data&lt;/em&gt; in your root directory then create an &lt;em&gt;AppDbContext&lt;/em&gt; Class in the Data folder and inherit from the &lt;em&gt;DbContext&lt;/em&gt; class (The DbContext class is an in-built class in .NET). Also, create a field in this class as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ApplicationDbContext : DbContext
    {

            public ApplicationDbContext(DbContextOptions&amp;lt;ApplicationDbContext&amp;gt; options) : base(options)
            {

            }

            public DbSet&amp;lt;Student&amp;gt; Students { get; set; }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Register a service in the program class as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddDbContext&amp;lt;ApplicationDbContext&amp;gt;(options =&amp;gt;
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Install Entity Framework Core Packages
&lt;/h2&gt;

&lt;p&gt;On the menu bar, click on &lt;em&gt;tools&lt;/em&gt;, select &lt;em&gt;NuGet package manager&lt;/em&gt; , then select &lt;em&gt;Manage NuGet Packages For solution&lt;/em&gt; . Click &lt;em&gt;browse&lt;/em&gt; and install the following packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Microsoft.EntityFramewrokCore&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Microsoft.EntityFramewrokCore.SqlServer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Microsoft.EntityFramewrokCore.Tools&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr949tsxik9qgngxdef0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr949tsxik9qgngxdef0.png" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wr949tsxik9qgngxdef0.png" width="800" height="248"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Ensure you install the version of each package that relates to the .NET Framework installed on your system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Add Migration
&lt;/h2&gt;

&lt;p&gt;On the menu bar, click on &lt;em&gt;tools&lt;/em&gt;, select &lt;em&gt;NuGet package manager&lt;/em&gt;, then select  &lt;em&gt;Package Manager console&lt;/em&gt;.&lt;br&gt;
On the console type &lt;em&gt;"add-migration"&lt;/em&gt; space &lt;em&gt;"AddStudentTable"&lt;/em&gt; (this is the name of the migration, you can use any name of your choice) then press enter. &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6drp7cuktsi2pj3ervdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6drp7cuktsi2pj3ervdl.png" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6drp7cuktsi2pj3ervdl.png" width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When, the build is done, on the Package Manager console, type &lt;em&gt;"update-database"&lt;/em&gt; then press enter.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftlavij3a84bc1scqme4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftlavij3a84bc1scqme4e.png" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlavij3a84bc1scqme4e.png" width="800" height="563"&gt;&lt;/a&gt;&lt;br&gt;
Once you see this, you're done! Congrats! You understand migrations in .NET using Entity Framework core!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As easy as these steps seem, I didn't find it funny a while ago. I encountered a serious issue because I couldn't get the accurate name of my local server and hence my "connection string" was faulty. I also had little idea of the AppDbContext class I was suppose to create to enable a smooth migration. I kept seeing error messages on my Package Manager console, but I figured it out in the end.&lt;br&gt;
I'm loving the whole programming experience as I've seen myself transform from a "hello world" novice to where I am now. &lt;br&gt;
I am also very excited to be a part of the &lt;strong&gt;HNG Internship 11&lt;/strong&gt;. I've heard so much about how this internship program pushes individuals to explore their untapped potentials. I opted in for this internship when I came across the opportunity because I want to ace my programming skills, become a better developer, meet other developers and great minds. I'm really anticipating what it holds. &lt;br&gt;
I will also advice individuals that wish to take their tech journey to the next level to apply for free via &lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt; &lt;br&gt;
There's also a premium package that gives you more opportunities. You can apply here  &lt;a href="https://hng.tech/premium"&gt;https://hng.tech/premium&lt;/a&gt;&lt;br&gt;
HNG also offers the best tech talents for individuals looking to hire for their firm &lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
