{"id":1980,"date":"2021-06-15T12:00:00","date_gmt":"2021-06-15T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/build-a-complete-app-with-asp-net-c-sql-lite-part-4\/"},"modified":"2026-07-05T03:25:46","modified_gmt":"2026-07-05T01:25:46","slug":"build-a-complete-app-with-asp-net-c-sql-lite-part-4","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/build-a-complete-app-with-asp-net-c-sql-lite-part-4\/","title":{"rendered":"Build a Complete App with ASP.Net, C#, SQL Lite \u2013 Part 4"},"content":{"rendered":"<p>This is part 4 of our complete application in ASP.Net, C# and SQL Lite. In this part, we would configure our application to run the migration at startup.<\/p>\n<p>We&#8217;ll also create Seeders (scripts that inserts some initial data to the database) and make these seeders run at startup.<\/p>\n<p>So let&#8217;s dive right in!<\/p>\n<ol>\n<li><a href=\"#t1\">About Web Host\u00a0 and Web Host Extension<\/a><\/li>\n<li><a href=\"#t2\">Create the Web Host Extension<\/a><\/li>\n<li><a href=\"#t3\">Create the Seeders<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">About Web Host and Web Host Extension<\/strong><\/h5>\n<p>First, what is the WebHost? This is the class responsible for handling application startup\u00a0 and lifetime management. This includes logging, dependency injection and configuration.<\/p>\n<p>You can take a look by opening the Startup.cs file<\/p>\n<p>Now we would like to extend this class, so that when the application starts up, the migration and seeders are run as well.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">Create the Web Host Extension<\/strong><\/h5>\n<p>We would place this inside a folder called extensions. So follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a folder called Extensions<\/p>\n<p><strong>Step 2<\/strong> &#8211; Inside this folder, create a class called WebHostExtensions. The content of this class is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">WebHostExtensions<\/span>\r\n{\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> IHost <span style=\"color: #0066bb; font-weight: bold;\">Initialize<\/span>( <span style=\"color: #008800; font-weight: bold;\">this<\/span> IHost host)\r\n    {\r\n        using(<span style=\"color: #333399; font-weight: bold;\">var<\/span> scope = host.Services.CreateScope())\r\n        {\r\n            <span style=\"color: #333399; font-weight: bold;\">var<\/span> services = scope.ServiceProvider;\r\n            <span style=\"color: #333399; font-weight: bold;\">var<\/span> context = services.GetRequiredService&lt;HospitalContext&gt;();\r\n            context.Database.Migrate();\r\n        }\r\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> host;\r\n    }\r\n}\r\n<\/pre>\n<p>See the video for explanation.<\/p>\n<p><strong>Step 3<\/strong> &#8211; Open your Program.cs file and adjust your main method like so<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Main<\/span>(<span style=\"color: #333399; font-weight: bold;\">string<\/span>[] args)\r\n{\r\n    <span style=\"color: #333399; font-weight: bold;\">var<\/span> host = CreateHostBuilder(args).Build();\r\n    host.Initialize().Run();\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Now you can run the program everything runs fine as well. Now delete the database and remove the connection. Run the program again, you will notice that the database is created again and all the tables are created as well.<\/p>\n<h5><strong id=\"t3\">Create the Seeders<\/strong><\/h5>\n<p>So basically, we use seeders to add some initial data to the database. So for each of the table, we would have class. All of them would be in a folder. Let&#8217;s follow the steps;<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a folder called Seeders.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Inside this folder, create a file called CountrySeeder (we start with this). The content of the CountrySeeder class is given below. So just copy and paste it.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">CountrySeeder<\/span>\r\n{\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">readonly<\/span> HospitalContext _context;\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">CountrySeeder<\/span>(HospitalContext context)\r\n    {\r\n        _context = context;\r\n    }\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Seed<\/span>()\r\n    {\r\n        AddNewCountry(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Country { CountryId = <span style=\"color: #6600ee; font-weight: bold;\">1<\/span>, Name = <span style=\"background-color: #fff0f0;\">\"India\"<\/span>, Capital = <span style=\"background-color: #fff0f0;\">\"New Delhi\"<\/span> });\r\n        AddNewCountry(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Country { CountryId = <span style=\"color: #6600ee; font-weight: bold;\">2<\/span>, Name = <span style=\"background-color: #fff0f0;\">\"Nigeria\"<\/span>, Capital = <span style=\"background-color: #fff0f0;\">\"Abuja\"<\/span> });\r\n        AddNewCountry(<span style=\"color: #008800; font-weight: bold;\">new<\/span> Country { CountryId = <span style=\"color: #6600ee; font-weight: bold;\">3<\/span>, Name = <span style=\"background-color: #fff0f0;\">\"Hungary\"<\/span>, Capital = <span style=\"background-color: #fff0f0;\">\"Budapest\"<\/span> });\r\n        _context.SaveChanges();\r\n    }\r\n\r\n    <span style=\"color: #888888;\">\/\/ since we run this seeder when the app starts<\/span>\r\n    <span style=\"color: #888888;\">\/\/ we avoid adding duplicates, so check first, then add<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">AddNewCountry<\/span>(Country country)\r\n    {\r\n        <span style=\"color: #333399; font-weight: bold;\">var<\/span> existingType = _context.Countries.FirstOrDefault(c =&gt; c.CountryId == country.CountryId);\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span> (existingType == <span style=\"color: #008800; font-weight: bold;\">null<\/span>)\r\n        {\r\n            _context.Countries.Add(country);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Watch the video for an explanation of the codes.<\/p>\n<p><strong>Step 5<\/strong> &#8211; Open the WebhostExtensions class and add the code below. Add it immediately under the context.Database.Migrate() line.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Lets not seed the database by calling the seeders<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">CountrySeeder<\/span>(context).Seed();\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong> &#8211; Launch the application again. If everything works well, you can stop it. Now\u00a0 if you open the database table you will see the three records we inserted as shown below:<\/p>\n<figure id=\"attachment_14684\" aria-describedby=\"caption-attachment-14684\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Records-Seeded-to-the-Countries-Table.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-14684\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Records-Seeded-to-the-Countries-Table-300x156.jpg\" alt=\"Records Seeded to the Countries Table\" width=\"300\" height=\"156\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Records-Seeded-to-the-Countries-Table-300x156.jpg 300w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Records-Seeded-to-the-Countries-Table-768x399.jpg 768w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Records-Seeded-to-the-Countries-Table-600x312.jpg 600w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/06\/Records-Seeded-to-the-Countries-Table.jpg 810w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-14684\" class=\"wp-caption-text\">Records Seeded to the Countries Table<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Follow the same method to create the 7 remaining seeders. You can get the completed codes in the link in the video description<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part 4 of our complete application in ASP.Net, C# and SQL Lite. In this part, we would configure our application to run the &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1980","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1980"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1980\/revisions"}],"predecessor-version":[{"id":2148,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1980\/revisions\/2148"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}