I'm currently following a C# tutorial, and it has this ApplicationDbContext class file:
public class ApplicationDbContext: DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {}
public DbSet<Category> Category { get; set; }
}
Where Category is defined as:
public class Category
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
public int DisplayOrder { get; set; }
}
Which is then later referenced like so:
private readonly ApplicationDbContext _db;
public IEnumerable<Category> Categories { get; set; }
public IndexModel(ApplicationDbContext db)
{
_db = db;
}
public void OnGet()
{
Categories = _db.Category;
}
I also have this code in the Program.cs file:
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")
));
The final piece of info is that I did a migration to create a database table called Category.
My confusion is around how everything is declared here; the line in the DbContext file with DbSet doesn't seem to instantiate anything and instead just defines Category as a public variable with type DbSet<Category>, but then the IEnumerable<Category> seems to treat Category as if it's going through the table, as does calling _db.Category.
In this case, looking through the code, the Category class is what gets referenced and not the Category database table. How does everything tie together here?