Search⌘ K
AI Features

Challenge: Save Data

Practice adding, updating, and deleting data in a school database using Entity Framework Core. This lesson guides you through performing insert and delete operations on various entities, including handling related data like departments and instructors. It helps you understand how to manage data changes effectively with EF Core.

We'll cover the following...

Overview

Using the project below, perform the tasks highlighted in the requirements section.

using Microsoft.EntityFrameworkCore;
using SaveData.Models.Entities;

namespace SaveData.Models.Data
{
    public class SchoolContext : DbContext
    {
        public SchoolContext()
        {

        }

        public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
        {

        }

        public virtual DbSet<Instructor> Instructors { get; set; }
        public virtual DbSet<Student> Students { get; set; }
        public virtual DbSet<Department> Departments { get; set; }
        public virtual DbSet<Course> Courses { get; set; }
        public virtual DbSet<StudentCourse> StudentCourses { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlite("data source=output/School.db");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Instructor>()
                        .HasOne(d => d.Department)
                        .WithMany(i => i.Instructors)
                        .HasForeignKey(inst => inst.DepartmentCode);

            modelBuilder.Entity<StudentCourse>().HasKey(sc => new { sc.StudentId, sc.CourseId });

            modelBuilder
                .Entity<StudentCourse>()
                .HasOne(sc => sc.Student)
                .WithMany(s => s.StudentCourses)
                .HasForeignKey(sc => sc.StudentId);

            modelBuilder
                .Entity<StudentCourse>()
                .HasOne(sc => sc.Course)
                .WithMany(c => c.StudentCourses)
                .HasForeignKey(sc => sc.CourseId);
        }

    }
}
Save data

After the required code is implemented, execute the ...