Setting up the DbContext

To interact with data as objects/entity classes, Entity Framework Core uses the Microsoft.EntityFrameworkCore.DbContext class, also called DbContext or simply Context. This class is in charge of all the entity objects during runtime, including populating them with data from the Database, keeping track of changes, and persisting them to the Database during CRUD operations.

We can easily create our very own DbContext class for our project--which we will call ApplicationDbContext--by doing the following:

  1. From Solution Explorer, right-click on the /Data/ folder we created a while ago and add a new ApplicationDbContext.cs class file.
  2. Fill it up with the following code:
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace TestMakerFreeWebApp.Data
{
public class ApplicationDbContext : DbContext
{
#region Constructor
public ApplicationDbContext(DbContextOptions options) :
base(options)
{
}
#endregion Constructor

#region Methods
protected override void OnModelCreating(ModelBuilder
modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<ApplicationUser>().ToTable("Users");
modelBuilder.Entity<ApplicationUser>().HasMany(u =>
u.Quizzes).WithOne(i => i.User);

modelBuilder.Entity<Quiz>().ToTable("Quizzes");
modelBuilder.Entity<Quiz>().Property(i =>
i.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Quiz>().HasOne(i => i.User).WithMany(u
=> u.Quizzes);
modelBuilder.Entity<Quiz>().HasMany(i =>
i.Questions).WithOne(c => c.Quiz);

modelBuilder.Entity<Question>().ToTable("Questions");
modelBuilder.Entity<Question>().Property(i =>
i.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Question>().HasOne(i =>
i.Quiz).WithMany(u => u.Questions);
modelBuilder.Entity<Question>().HasMany(i =>
i.Answers).WithOne(c => c.Question);

modelBuilder.Entity<Answer>().ToTable("Answers");
modelBuilder.Entity<Answer>().Property(i =>
i.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Answer>().HasOne(i =>
i.Question).WithMany(u => u.Answers);

modelBuilder.Entity<Result>().ToTable("Results");
modelBuilder.Entity<Result>().Property(i =>
i.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Result>().HasOne(i =>
i.Quiz).WithMany(u => u.Results);
}
#endregion Methods

#region Properties
public DbSet<ApplicationUser> Users { get; set; }
public DbSet<Quiz> Quizzes { get; set; }
public DbSet<Question> Questions { get; set; }
public DbSet<Answer> Answers { get; set; }
public DbSet<Result> Results { get; set; }
#endregion Properties
}
}

There are a couple of important things we did here:

  • We overrode the OnModelCreating method to manually define our Data Model relationships for our entity classes. Note that we manually configured the table names for each entity using the modelBuilder.Entity<TEntityType>().ToTable method; we did that with the sole purpose of showing how easy it is to customize the Code-First generated Database.
  • We added a DbSet<T> property for each of our entities, so we can easily access them later on.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.35