Step 1 - Create Models and Migrations (C# Project on Classroom Voting Application)

This is the first step of writing the code for the classroom voting application. In this tutorial we shall add a model class to hold voting data. We shall also add a project context for EF Core connectivity. Finally, we shall add migrations, and then apply the migrations. We shall then verify that the database get created. There are two pre-requisites that you must be aware because I shall assume that you are already familiar with C#. For this you can refer the list of my courses and locate the course on C#. Secondly you must also go through the chapter on Entity Framework Core for Console Applications.
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

Starting from Scratch

First of all create a basic C# .NET Core console project that targets the latest version of C# 11, and also the latest version of .NET Core.

I have attached the source code of this project to the downloads. The project is completed till the progress made in this tutorial.

Open the program.cs file and print a hello world message.

Run this project to verify that it compiles and outputs the message.

Video Explanation (see it happen!)

Please watch the following youtube video:

Add Model Class

Next add a folder called Models and then add a class called Vote so that we can see a vote.cs file in the solution explorer.

Open the package manager console and add two nuget packages.


// add these one by one 

PM> Install-Package Microsoft.EntityFrameworkCore.Sqlite

PM> Install-Package Microsoft.EntityFrameworkCore.Tools

We have decided to use sqlite because of portability and ease of use.

Allow the nuget downloads to complete till success. We expect you already know about nuget packages, and also about applying them.

Add Model Class

Let us now complete the vote.cs file.

First add a record class called Vote. We have marked it with an attribute called Table so that a table called Vote is created in the database. This wasn't really necessary - but we have done it for tutorial purposes.

Similarly a primary key for StudentID has been added.

The string Choice property holds the choice selected by a student, such as a, b, c, d, e or anything similar.

A DbContext is added to model the database.

The records are represented by a DbSet of votes.


// THIS CODE IS INDICATIVE  
// MAY NOT COMPILE 

// FULL Source code is in the downloads 

// refer "Entity Framework Core for Console Applications" 
// of the C# Course  

[Table("Vote")]
public record class Vote
{
  [Key]
  [Column("StudentID")]
  public string StudentID { get; set; } = default!;

  [Column("Choice")]
  public string Choice { get; set; } = default!;
}

// DbContext that maps to the database  

public class ProjectContext : DbContext
{
  public DbSet<Vote> Votes { get; set; } = default!;

  private readonly string _dbPath;

  public ProjectContext()
  {
    // the source code is in the downloads 
  }

  protected override void OnConfiguring(...)
  {
    options.UseSqlite($"Data Source={_dbPath}");
  }
}

The path to the database can be stored in the dbPath property. The ProjectContext constructor sets the path to the current working directory which shall create the file in the same directory where the executable will be compiled. It is just a matter of convenience. We could have set it to any convenient path.

The OnConfiguring override sets the connection string to the sqlite database.

Add Migration

Open the package manager command prompt and type the command for Add-Migration Initial_Create


PM> Add-Migration Initial_Create

// after that run this command - see video 

PM> Update-Database

Hit enter and allow it to complete. If everything goes right, a folder called Migrations will appear in the solution explorer. This means that the migration was created successfully and everything is fine so far.

Again come to the package manager console and type the command Update-Database to apply the migrations.

Hit enter and allow it to complete.

You should now be able to see the database file in the output directory. This file is in the same folder where the executable has been created.

This completes the first step.

Now you should download the source code completed till this stage, and examine to ensure that you understand the code so far. Thanks!


This Blog Post/Article "Step 1 - Create Models and Migrations (C# Project on Classroom Voting Application)" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.