Home > asp.net, C# Coding, Making Waves, Software, Uncategorized > Making Waves: Building a Database in an asp.net core web app.

Making Waves: Building a Database in an asp.net core web app.

Now, suppose you need to create a database that is accessible as a web app or server app. You have a copy of Visual Studio 2022 on your PC/laptop and you enjoy using C# as a programming language. You may have dabbled with LINQ to create some CRUD apps some time ago to save some XML files to the drive but these just aren’t going to cut it with a web based app. What follows are instructions for creating a basic database app with a SQL database using Entity Framework Core and asp.net core MVC.

Firstly, open up Visual Studio and click on Create New Project. In the Create New Project page shown above, click on ASP.NET Core Web App (Model-View-Controller) and then click Next.

On the following page choose a name for your project. This can be anything but it pays to use a name relevant to what your project is for. Then click Next.

Choose your framework from the drop down menu, making sure it is the most recent one and then click next. You should now have a screen similar to the following image

If you look in Solution Explorer, you will see a list of folders used in the project. Find the Models folder, right click on it and select <<Add>> then <<Class>>. This class will be the model for your database so name it something relevant. I’m going to create a demo personnel database so will call the class Person.cs. The completed Class should look something like the following – public int Id is necessary as this will be the primary key. However, choose other variables to suit whatever database you are creating.

using System.ComponentModel.DataAnnotations;

namespace DBDemo.Models
{
    public class Person
    {
        [Required]public int Id { get; set; }
        [Required] public string Name { get; set; }
        public int Age { get; set; }
        public int Telephone { get; set; }
        public string Occupation { get; set; }
    }
}

Next, using the NuGet Package Manager Tools, install Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.SqlServer. These are necessary before moving on to the next stage. Once these have installed, return to the Solution Explorer and this time, right click on Controllers. In the drop down menu, click <<Add>> <<New Scaffolded Item>> <<MVC Controller with views using Entity Framework>> <<Add>>. On the form use the drop down menu at the top to select the Model class you previously created, press the + button neside the DbContext class and it will create one for you, then click Add.

The scaffolding process may take a few minutes to complete, depending on the size of your model and how many fields you are using. Once completed you will see the controller class on your screen. Build the solution (F6) and ensure there are no errors. Then open up your Project Manager Console and type:

Add-Migration InitialCreate

Once complete type:

Update-Database.

In the Solution Explorer, expand Views and you will see your new database listed here. Now click Start Without Debugging (Ctrl + F5). This should produce a Welcome screen on your local webserver. The URL will be something like https://localhost:7204/ append the name of your database view to the end i.e. https://localhost:7204/People and you will see your new, empty database with a create new hyperlink at the top. Click on this and add a record then return to Index. If it looks something like the following, well done you have followed the instructions and created your database.

Thank you for taking the time to read this, I hope it was helpful. Until next time…

  1. No comments yet.
  1. No trackbacks yet.

Leave a comment