Adding DBContext to an ASP.NET application

We just added the Todo model. Now, let's add DBContext to manage and persist Todo in the database. DBContext acts as a bridge between your classes and database. To add it, follow these steps:

  1. Right-click on the Models folder and navigate to Add | Class:
Add a class for DBContext under the Models folder
  1. Name the class as TodoContext and add the following code snippet to it:
   public class TodoContext : DbContext   {     public TodoContext(DbContextOptions<TodoContext>            options)     : base(options)    {    }    public DbSet<Todo> Todos { get; set; }  }

TodoContext helps you interact with the database and commits the ...

Get Learning Angular for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.