So start by creating a new project in Visual Studio. At this time the latest version of Visual Studio is 2022 and I am using the Community Edition, which is free. .NET Framework Core 7 just released, so we will use the latest versions.

We will create a basic example with single table named Person. Person table will have few basic columns like name, gender and phone number.

Configure your new project!

New ASP.NET Core Web API project in Visual Studio 2022 Choose all the default options in the next step. Notable options are below:

Framework: .NET 7.0 Authentication type: None Configure for HTTPS: Yes Enable Docker: No Use controllers: Yes Enable OpenAPI support: Yes

Web API project with Repository pattern

We will use Repository pattern throughout all the tutorials. The repository layer will only include all the database (Entity Framework) related code. On top of the repository pattern, we will add a service layer, which will include business/domain logic code. And finally, the Web API controllers will be using the service classes.

Repository pattern!

For a very small project, which we are going to build here now, this really seems a lot of effort. But patterns and architectures promote reusability and better software maintenance, and learning that is much more important than programming syntax.

Person Entity class

Lets start the coding work by creating the Entity class first. Entity classes define the database schema. All database tables, columns, relationships etc. are defined in the Entity classes.

Create a new folder Entities in Person project. In Entities folder, add a new class Person.

We also need to add the following EF Core related packages from NuGet. All these packages are developed by Microsoft.

  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

After you install the above packages, the NuGet Installed tab should look like below.

EF-Core-nuget-packages!

Back to Entities\Person.cs class, add the Table annotation for the class. This means our table name in SQL Server database will be “Person”.

[Table("Person")]
public class Person
{
}

Next, we will add some fields in the Person table. In entity classes, fields are defined as properties. We also use annotations to pass extra information to the database. Update Person.cs class as below.

[Table("Person")]
public class Person
{
  [Key]
  public int PersonId { get; set; }
  [Required, MaxLength(100)]
  public string? FirstName { get; set; }
  [Required, MaxLength(100)]
  public string? LastName { get; set; }
  [MaxLength(20)]
  public string PhoneNumber { get; set; } = "";
  [MaxLength(1)]
  public string Gender { get; set; } = "";
}

This is the way how we define schema in Entity classes. The Entity Framework generates the database based on these classes. This is called Code first approach. We define schema in code, the actual database is generated later.

There are few annotations we used in Person class, lets quickly go through each

  • Table(“Person”) – Name of the table in the database
  • Key – Primary key of the table. In this case the primary key is of type int and it is auto generated by the SQL Server.
  • Required – Required fields in the table, cannot be NULL. Can have empty string or some value.
  • MaxLength – Maximum number of characters for the field

Create DbContext class to create database

So far we created Person class to define the table schema. Next we need to use the Entity Framework’s DbContext class, to actually create the database and tables.

In the main project, add a new folder Data. Create a new class AppDbContext in the Data folder. It will inherit from Microsoft.EntityFrameworkCore.DbContext class.

We will also create a constructor where options (connectionstring) will be passed.

The database context class also lists the tables, as public properties of type DbSet. See the example below to get a clear idea.

public class AppDbContext : DbContext
{
  public AppDbContext(DbContextOptions<AppDbContext> options)
      : base(options)
  {
  }

  // Tables
  public DbSet<Entities.Person>? Persons { get; set; }
}

Note that we use plural for table properties. If entity (table) class is Person, we will use Persons. If entity class is Address, we will use Addresses in DbContext property. It is just a convention. You can choose any name you like. We use plural because it is actually a collection.

DbContext.Persons is a collection, having n number of records.

Are we ready to generate the database? Not yet. We need to give EF Core the database connection string. And we also need to keep the connection string at a secure place (not part of project).

Continue in next article.